Handle library match failure up front in analyze()
This CL makes analyze() less confusing. It took me a very long time
reading it to remember why it handled the case of failing to parse the
library declaration. So I moved it to the top and added a comment.
Change-Id: I8ce78559f18a08edd1705535f96cead28fd1a03f
Reviewed-on: https://fuchsia-review.googlesource.com/c/fidlbolt/+/587921
Reviewed-by: Yifei Teng <yifeit@google.com>
diff --git a/backend/analyze.go b/backend/analyze.go
index 9073a25..7ccf018 100644
--- a/backend/analyze.go
+++ b/backend/analyze.go
@@ -106,12 +106,21 @@
// cannot be located in a.paths, it is silently ignored.
func (a *libraryAnalyzer) analyze(filename, content string) (libraryMap, []annotation) {
result := make(libraryMap)
+ rootLibraryMatch, ok := parseLibraryMatch(content)
+ if !ok {
+ // If we can't parse the library name, there's no point in continuing.
+ // We could make this function return an error and report that to the
+ // user. However, we prefer to invoke fidlc anyway and show its error.
+ // So, we return a libraryName with an empty library name such that
+ // fidlcFileArgs() will produce --files filename.
+ result[""] = libraryInfo{files: []string{filename}}
+ return result, nil
+ }
// Start with a stack containing the root library (if it's a platform source
// library, meaning we can expect to find it in the tree) and its imports.
var stack []library
- rootLibraryMatch, rootLibraryOk := parseLibraryMatch(content)
- if rootLibraryOk && rootLibraryMatch.lib.isPlatform() {
+ if rootLibraryMatch.lib.isPlatform() {
stack = append(stack, rootLibraryMatch.lib)
}
rootImports := parsePlatformImportsMatch(content)
@@ -191,12 +200,8 @@
// 2. The user typed something like `library fuchsia.io;`. This behaves as
// if they were adding a new file to the fuchsia.io library, alongside
// the other ones in the source tree.
- rootLib := rootLibraryMatch.lib
- if !rootLibraryOk {
- // The name doesn't really matter. We just need an entry in the map.
- rootLib = "fidlbolt"
- }
var anns []annotation
+ rootLib := rootLibraryMatch.lib
rootInfo := libraryInfo{files: []string{filename}}
for _, m := range rootImports {
rootInfo.deps = append(rootInfo.deps, m.lib)
@@ -207,11 +212,9 @@
}
if info, ok := result[rootLib]; ok {
rootInfo = mergeInfo(rootInfo, info)
- if rootLibraryOk {
- text := fmt.Sprintf("Found %s in %s\n\n%s",
- rootLib, info.userVisibleDir(), existingLibraryNotice)
- anns = append(anns, rootLibraryMatch.annotation(Info, text))
- }
+ text := fmt.Sprintf("Found %s in %s\n\n%s",
+ rootLib, info.userVisibleDir(), existingLibraryNotice)
+ anns = append(anns, rootLibraryMatch.annotation(Info, text))
}
result[rootLib] = rootInfo