Add support for go:build style constraints

This doesn't account for the new syntax, but there was no syntax
parsing in the old version anyway, and the only user doesn't currently
care about semantics
This commit is contained in:
Sauyon Lee
2021-08-17 09:42:26 -07:00
parent f39e43e5d0
commit 189070cf2c
2 changed files with 12 additions and 4 deletions

View File

@@ -189,7 +189,7 @@ private Comment getInitialComment(File f, int i) {
}
/**
* A build constraint comment of the form `// +build ...`.
* A build constraint comment of the form `// +build ...` or `//go:build ...`.
*
* Examples:
*
@@ -203,17 +203,23 @@ class BuildConstraintComment extends LineComment {
// a line comment preceding the package declaration, itself only preceded by
// line comments
exists(File f, int i |
// correctness of the placement of the build constraint is not checked here;
// this is more lax than the actual rules for build constraints
this = getInitialComment(f, i) and
not getInitialComment(f, [0 .. i - 1]) instanceof BlockComment
) and
// comment text starts with `+build`
getText().regexpMatch("\\s*\\+build.*")
(
// comment text starts with `+build` or `go:build`
this.getText().regexpMatch("\\s*\\+build.*")
or
this.getText().regexpMatch("\\s*go:build.*")
)
}
override string getAPrimaryQlClass() { result = "BuildConstraintComment" }
/** Gets the body of this build constraint. */
string getConstraintBody() { result = getText().splitAt("+build ", 1) }
string getConstraintBody() { result = getText().splitAt("build ", 1) }
/** Gets a disjunct of this build constraint. */
string getADisjunct() { result = getConstraintBody().splitAt(" ") }

View File

@@ -222,6 +222,8 @@ class ExtractedOrExternalFile extends Container, @file, Documentable, ExprParent
exists(BuildConstraintComment bcc | this = bcc.getFile() |
forex(string disjunct | disjunct = bcc.getADisjunct() |
disjunct.splitAt(",").(Architecture).getBitSize() = bitSize
or
disjunct.splitAt("/").(Architecture).getBitSize() = bitSize
)
)
}