From 189070cf2ced2cb2bb7c7f226d81237c56875564 Mon Sep 17 00:00:00 2001 From: Sauyon Lee Date: Tue, 17 Aug 2021 09:42:26 -0700 Subject: [PATCH] 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 --- ql/src/semmle/go/Comments.qll | 14 ++++++++++---- ql/src/semmle/go/Files.qll | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ql/src/semmle/go/Comments.qll b/ql/src/semmle/go/Comments.qll index 2a765c5e972..e072c0cb7ca 100644 --- a/ql/src/semmle/go/Comments.qll +++ b/ql/src/semmle/go/Comments.qll @@ -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(" ") } diff --git a/ql/src/semmle/go/Files.qll b/ql/src/semmle/go/Files.qll index ed27c6b557b..92981b74711 100644 --- a/ql/src/semmle/go/Files.qll +++ b/ql/src/semmle/go/Files.qll @@ -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 ) ) }