diff options
Diffstat (limited to 'src/util/make_errors.go')
-rw-r--r-- | src/util/make_errors.go | 98 |
1 files changed, 79 insertions, 19 deletions
diff --git a/src/util/make_errors.go b/src/util/make_errors.go index cc35409..dc8039a 100644 --- a/src/util/make_errors.go +++ b/src/util/make_errors.go @@ -25,6 +25,7 @@ import ( "sort" "strconv" "strings" + "unicode" ) // ssl.h reserves values 1000 and above for error codes corresponding to @@ -60,11 +61,12 @@ func makeErrors(reset bool) error { } prefix := strings.ToUpper(lib) - reasons, err := parseHeader(prefix, headerFile) + functions, reasons, err := parseHeader(prefix, headerFile) headerFile.Close() if reset { err = nil + functions = make(map[string]int) // Retain any reason codes above reservedReasonCode. newReasons := make(map[string]int) for key, value := range reasons { @@ -95,11 +97,12 @@ func makeErrors(reset bool) error { continue } - if err := addReasons(reasons, name, prefix); err != nil { + if err := addFunctionsAndReasons(functions, reasons, name, prefix); err != nil { return err } } + assignNewValues(functions, -1) assignNewValues(reasons, reservedReasonCode) headerFile, err = os.Open(headerPath) @@ -114,7 +117,7 @@ func makeErrors(reset bool) error { } defer newHeaderFile.Close() - if err := writeHeaderFile(newHeaderFile, headerFile, prefix, reasons); err != nil { + if err := writeHeaderFile(newHeaderFile, headerFile, prefix, functions, reasons); err != nil { return err } os.Rename(headerPath+".tmp", headerPath) @@ -124,7 +127,8 @@ func makeErrors(reset bool) error { return err } - outputStrings(dataFile, lib, reasons) + outputStrings(dataFile, lib, typeFunctions, functions) + outputStrings(dataFile, lib, typeReasons, reasons) dataFile.Close() return nil @@ -132,16 +136,16 @@ func makeErrors(reset bool) error { func findToplevel() (path string, err error) { path = ".." - buildingPath := filepath.Join(path, "BUILDING.md") + buildingPath := filepath.Join(path, "BUILDING") _, err = os.Stat(buildingPath) if err != nil && os.IsNotExist(err) { path = filepath.Join("..", path) - buildingPath = filepath.Join(path, "BUILDING.md") + buildingPath = filepath.Join(path, "BUILDING") _, err = os.Stat(buildingPath) } if err != nil { - return "", errors.New("Cannot find BUILDING.md file at the top-level") + return "", errors.New("Cannot find BUILDING file at the top-level") } return path, nil } @@ -179,7 +183,7 @@ func outputAssignments(w io.Writer, assignments map[string]int) { } } -func parseDefineLine(line, lib string) (key string, value int, ok bool) { +func parseDefineLine(line, lib string) (typ int, key string, value int, ok bool) { if !strings.HasPrefix(line, "#define ") { return } @@ -189,8 +193,16 @@ func parseDefineLine(line, lib string) (key string, value int, ok bool) { return } + funcPrefix := lib + "_F_" + reasonPrefix := lib + "_R_" + key = fields[1] - if !strings.HasPrefix(key, lib+"_R_") { + switch { + case strings.HasPrefix(key, funcPrefix): + typ = typeFunctions + case strings.HasPrefix(key, reasonPrefix): + typ = typeReasons + default: return } @@ -203,7 +215,7 @@ func parseDefineLine(line, lib string) (key string, value int, ok bool) { return } -func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, reasons map[string]int) error { +func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, functions, reasons map[string]int) error { var last []byte var haveLast, sawDefine bool newLine := []byte("\n") @@ -211,7 +223,7 @@ func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, reasons map[ scanner := bufio.NewScanner(headerFile) for scanner.Scan() { line := scanner.Text() - _, _, ok := parseDefineLine(line, lib) + _, _, _, ok := parseDefineLine(line, lib) if ok { sawDefine = true continue @@ -235,6 +247,7 @@ func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, reasons map[ return err } + outputAssignments(w, functions) outputAssignments(w, reasons) w.Write(newLine) @@ -246,9 +259,14 @@ func writeHeaderFile(w io.Writer, headerFile io.Reader, lib string, reasons map[ return nil } -func outputStrings(w io.Writer, lib string, assignments map[string]int) { +const ( + typeFunctions = iota + typeReasons +) + +func outputStrings(w io.Writer, lib string, ty int, assignments map[string]int) { lib = strings.ToUpper(lib) - prefixLen := len(lib + "_R_") + prefixLen := len(lib + "_F_") keys := make([]string, 0, len(assignments)) for key := range assignments { @@ -257,7 +275,11 @@ func outputStrings(w io.Writer, lib string, assignments map[string]int) { sort.Strings(keys) for _, key := range keys { - fmt.Fprintf(w, "%s,%d,%s\n", lib, assignments[key], key[prefixLen:]) + typeString := "function" + if ty == typeReasons { + typeString = "reason" + } + fmt.Fprintf(w, "%s,%s,%d,%s\n", lib, typeString, assignments[key], key[prefixLen:]) } } @@ -317,7 +339,7 @@ func handleDeclareMacro(line, join, macroName string, m map[string]int) { } } -func addReasons(reasons map[string]int, filename, prefix string) error { +func addFunctionsAndReasons(functions, reasons map[string]int, filename, prefix string) error { file, err := os.Open(filename) if err != nil { return err @@ -325,15 +347,45 @@ func addReasons(reasons map[string]int, filename, prefix string) error { defer file.Close() reasonPrefix := prefix + "_R_" + var currentFunction string scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() + if len(line) > 0 && unicode.IsLetter(rune(line[0])) { + /* Function start */ + fields := strings.Fields(line) + for _, field := range fields { + if i := strings.Index(field, "("); i != -1 { + f := field[:i] + // The return type of some functions is + // a macro that contains a "(". + if f == "STACK_OF" { + continue + } + currentFunction = f + for len(currentFunction) > 0 && currentFunction[0] == '*' { + currentFunction = currentFunction[1:] + } + break + } + } + } + + // Do not include cross-module error lines. + if strings.Contains(line, "OPENSSL_PUT_ERROR(" + prefix + ",") { + functionToken := prefix + "_F_" + currentFunction + if _, ok := functions[functionToken]; !ok { + functions[functionToken] = -1 + } + } + handleDeclareMacro(line, "_R_", "OPENSSL_DECLARE_ERROR_REASON(", reasons) + handleDeclareMacro(line, "_F_", "OPENSSL_DECLARE_ERROR_FUNCTION(", functions) for len(line) > 0 { - i := strings.Index(line, prefix+"_") + i := strings.Index(line, prefix + "_") if i == -1 { break } @@ -361,17 +413,25 @@ func addReasons(reasons map[string]int, filename, prefix string) error { return scanner.Err() } -func parseHeader(lib string, file io.Reader) (reasons map[string]int, err error) { +func parseHeader(lib string, file io.Reader) (functions, reasons map[string]int, err error) { + functions = make(map[string]int) reasons = make(map[string]int) scanner := bufio.NewScanner(file) for scanner.Scan() { - key, value, ok := parseDefineLine(scanner.Text(), lib) + typ, key, value, ok := parseDefineLine(scanner.Text(), lib) if !ok { continue } - reasons[key] = value + switch typ { + case typeFunctions: + functions[key] = value + case typeReasons: + reasons[key] = value + default: + panic("internal error") + } } err = scanner.Err() |