diff --git a/ql/src/semmle/go/Errors.qll b/ql/src/semmle/go/Errors.qll index 3c8a8b9adea..2f4e8543ee5 100644 --- a/ql/src/semmle/go/Errors.qll +++ b/ql/src/semmle/go/Errors.qll @@ -19,7 +19,7 @@ class Error extends @error { int getIndex() { errors(this, _, _, _, _, _, _, _, result) } /** Gets the file in which this error was reported, if it can be determined. */ - File getFile() { hasLocationInfo(result.getAbsolutePath(), _, _, _, _) } + DiagnosticFile getFile() { hasLocationInfo(result.getAbsolutePath(), _, _, _, _) } /** * Holds if this element is at the specified location. diff --git a/ql/src/semmle/go/Files.qll b/ql/src/semmle/go/Files.qll index 73c30605ff9..79e19d9d830 100644 --- a/ql/src/semmle/go/Files.qll +++ b/ql/src/semmle/go/Files.qll @@ -177,25 +177,12 @@ class Folder extends Container, @folder { override string getURL() { result = "folder://" + getAbsolutePath() } } -/** A file. */ -class File extends Container, @file, Documentable, ExprParent, GoModExprParent, DeclParent, - ScopeNode { - string path; - - File() { - files(this, path, _, _, _) and - ( - // Exclude `.go` files that have not been extracted. Non-extracted files only exist in the `files` - // table if we are reporting compilation errors relating to them in the `diagnostics` table. - not path.matches("%.go") - or - exists(this.getAChild()) - ) - } - +/** Any file, including files that have not been extracted but must exist as locations for errors. */ +class DiagnosticFile extends Container, @file, Documentable, ExprParent, GoModExprParent, + DeclParent, ScopeNode { override Location getLocation() { has_location(this, result) } - override string getAbsolutePath() { result = path } + override string getAbsolutePath() { files(this, result, _, _, _) } /** Gets the number of lines in this file. */ int getNumberOfLines() { numlines(this, result, _, _) } @@ -266,3 +253,14 @@ class File extends Container, @file, Documentable, ExprParent, GoModExprParent, override string getAPrimaryQlClass() { result = "File" } } + +/** A file that has been extracted. */ +class File extends DiagnosticFile { + File() { + // getAChild is specifically for the Go SAT and so does not apply to non-go files + // we care about all non-go extracted files, as only go files can have `@file` entries due to requiring a file entry for diagnostic errors + not this.getExtension() = "go" + or + exists(this.getAChild()) + } +}