From 1b502c161e59f724092f88d3e1fe357b23211c63 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Sat, 31 Oct 2020 11:51:01 +0100 Subject: [PATCH] Add Locations library and move language independent files to 'codeql' --- ql/src/codeql/Locations.qll | 53 +++++++++++++++++++ .../files/FileSystem.qll | 0 2 files changed, 53 insertions(+) create mode 100644 ql/src/codeql/Locations.qll rename ql/src/{codeql_ruby => codeql}/files/FileSystem.qll (100%) diff --git a/ql/src/codeql/Locations.qll b/ql/src/codeql/Locations.qll new file mode 100644 index 00000000000..7497525d7af --- /dev/null +++ b/ql/src/codeql/Locations.qll @@ -0,0 +1,53 @@ +/** Provides classes for working with locations. */ + +import files.FileSystem + +/** + * A location as given by a file, a start line, a start column, + * an end line, and an end column. + * + * For more information about locations see [LGTM locations](https://lgtm.com/help/ql/locations). + */ +class Location extends @location { + /** Gets the file for this location. */ + File getFile() { locations_default(this, result, _, _, _, _) } + + /** Gets the 1-based line number (inclusive) where this location starts. */ + int getStartLine() { locations_default(this, _, result, _, _, _) } + + /** Gets the 1-based column number (inclusive) where this location starts. */ + int getStartColumn() { locations_default(this, _, _, result, _, _) } + + /** Gets the 1-based line number (inclusive) where this location ends. */ + int getEndLine() { locations_default(this, _, _, _, result, _) } + + /** Gets the 1-based column number (inclusive) where this location ends. */ + int getEndColumn() { locations_default(this, _, _, _, _, result) } + + /** Gets the number of lines covered by this location. */ + int getNumLines() { result = getEndLine() - getStartLine() + 1 } + + /** Gets a textual representation of this element. */ + string toString() { + exists(string filepath, int startline, int startcolumn, int endline, int endcolumn | + hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and + result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn + ) + } + + /** + * Holds if this element is at the specified location. + * The location spans column `startcolumn` of line `startline` to + * column `endcolumn` of line `endline` in file `filepath`. + * For more information, see + * [LGTM locations](https://lgtm.com/help/ql/locations). + */ + predicate hasLocationInfo( + string filepath, int startline, int startcolumn, int endline, int endcolumn + ) { + exists(File f | + locations_default(this, f, startline, startcolumn, endline, endcolumn) and + filepath = f.getAbsolutePath() + ) + } +} diff --git a/ql/src/codeql_ruby/files/FileSystem.qll b/ql/src/codeql/files/FileSystem.qll similarity index 100% rename from ql/src/codeql_ruby/files/FileSystem.qll rename to ql/src/codeql/files/FileSystem.qll