mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
CPP: Autoformat CommentedOutCode.qll.
This commit is contained in:
@@ -17,30 +17,33 @@ import cpp
|
||||
*/
|
||||
bindingset[line]
|
||||
private predicate looksLikeCode(string line) {
|
||||
exists(string trimmed |
|
||||
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "") |
|
||||
trimmed.regexpMatch(".*[{};]")
|
||||
and (
|
||||
// If this line looks like code because it ends with a closing
|
||||
// brace that's preceded by something other than whitespace ...
|
||||
trimmed.regexpMatch(".*.\\}")
|
||||
implies
|
||||
// ... then there has to be ") {" (or some variation)
|
||||
// on the line, suggesting it's a statement like `if`
|
||||
// or a function declaration. Otherwise it's likely to be a
|
||||
// benign use of braces such as a JSON example or explanatory
|
||||
// pseudocode.
|
||||
trimmed.regexpMatch(".*(\\)|const|volatile|override|final|noexcept|&)\\s*\\{.*")
|
||||
)
|
||||
and not trimmed.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})"))
|
||||
exists(string trimmed |
|
||||
trimmed = line.regexpReplaceAll("(?i)(^\\s+|&#?[a-z0-9]{1,31};|\\s+$)", "")
|
||||
|
|
||||
trimmed.regexpMatch(".*[{};]") and
|
||||
(
|
||||
// If this line looks like code because it ends with a closing
|
||||
// brace that's preceded by something other than whitespace ...
|
||||
trimmed.regexpMatch(".*.\\}")
|
||||
implies
|
||||
// ... then there has to be ") {" (or some variation)
|
||||
// on the line, suggesting it's a statement like `if`
|
||||
// or a function declaration. Otherwise it's likely to be a
|
||||
// benign use of braces such as a JSON example or explanatory
|
||||
// pseudocode.
|
||||
trimmed.regexpMatch(".*(\\)|const|volatile|override|final|noexcept|&)\\s*\\{.*")
|
||||
) and
|
||||
not trimmed
|
||||
.regexpMatch("(>.*|.*[\\\\@][{}].*|(optional|repeated) .*;|.*(\\{\\{\\{|\\}\\}\\}).*|\\{[-0-9a-zA-Z]+\\})")
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The line of a C++-style comment within its file `f`.
|
||||
*/
|
||||
private int lineInFile(CppStyleComment c, File f) {
|
||||
f = c.getFile() and
|
||||
result = c.getLocation().getStartLine()
|
||||
f = c.getFile() and
|
||||
result = c.getLocation().getStartLine()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,11 +56,12 @@ private int lineInFile(CppStyleComment c, File f) {
|
||||
* without line comments would increase the line number without increasing
|
||||
* the rank and thus force a change of block ID.
|
||||
*/
|
||||
private pragma[nomagic] int commentLineBlockID(File f, int line) {
|
||||
exists(int lineRank |
|
||||
line = rank[lineRank](lineInFile(_, f)) and
|
||||
result = line - lineRank
|
||||
)
|
||||
pragma[nomagic]
|
||||
private int commentLineBlockID(File f, int line) {
|
||||
exists(int lineRank |
|
||||
line = rank[lineRank](lineInFile(_, f)) and
|
||||
result = line - lineRank
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,80 +71,80 @@ private pragma[nomagic] int commentLineBlockID(File f, int line) {
|
||||
* for separate runs.
|
||||
*/
|
||||
private int commentId(CppStyleComment c, File f, int line) {
|
||||
result = commentLineBlockID(f, line) and
|
||||
line = lineInFile(c, f)
|
||||
result = commentLineBlockID(f, line) and
|
||||
line = lineInFile(c, f)
|
||||
}
|
||||
|
||||
/**
|
||||
* A contiguous block of comments.
|
||||
*/
|
||||
class CommentBlock extends Comment {
|
||||
CommentBlock() {
|
||||
this instanceof CppStyleComment implies
|
||||
not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f))
|
||||
}
|
||||
CommentBlock() {
|
||||
this instanceof CppStyleComment
|
||||
implies
|
||||
not exists(CppStyleComment pred, File f | lineInFile(pred, f) + 1 = lineInFile(this, f))
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `i`th comment associated with this comment block.
|
||||
*/
|
||||
Comment getComment(int i) {
|
||||
(i = 0 and result = this) or
|
||||
exists(File f, int thisLine, int resultLine |
|
||||
commentId(this, f, thisLine) = commentId(result, f, resultLine) |
|
||||
i = resultLine - thisLine
|
||||
)
|
||||
}
|
||||
/**
|
||||
* Gets the `i`th comment associated with this comment block.
|
||||
*/
|
||||
Comment getComment(int i) {
|
||||
i = 0 and result = this
|
||||
or
|
||||
exists(File f, int thisLine, int resultLine |
|
||||
commentId(this, f, thisLine) = commentId(result, f, resultLine)
|
||||
|
|
||||
i = resultLine - thisLine
|
||||
)
|
||||
}
|
||||
|
||||
Comment lastComment() {
|
||||
result = this.getComment(max(int i | exists(this.getComment(i))))
|
||||
}
|
||||
Comment lastComment() { result = this.getComment(max(int i | exists(this.getComment(i)))) }
|
||||
|
||||
string getLine(int i) {
|
||||
this instanceof CStyleComment and result = this.getContents().regexpCapture("(?s)/\\*+(.*)\\*+/", 1).splitAt("\n", i)
|
||||
or
|
||||
this instanceof CppStyleComment and result = this.getComment(i).getContents().suffix(2)
|
||||
}
|
||||
string getLine(int i) {
|
||||
this instanceof CStyleComment and
|
||||
result = this.getContents().regexpCapture("(?s)/\\*+(.*)\\*+/", 1).splitAt("\n", i)
|
||||
or
|
||||
this instanceof CppStyleComment and result = this.getComment(i).getContents().suffix(2)
|
||||
}
|
||||
|
||||
int numLines() {
|
||||
result = strictcount(int i, string line | line = this.getLine(i) and line.trim() != "")
|
||||
}
|
||||
int numLines() {
|
||||
result = strictcount(int i, string line | line = this.getLine(i) and line.trim() != "")
|
||||
}
|
||||
|
||||
int numCodeLines() {
|
||||
result = strictcount(int i, string line | line = this.getLine(i) and looksLikeCode(line))
|
||||
}
|
||||
int numCodeLines() {
|
||||
result = strictcount(int i, string line | line = this.getLine(i) and looksLikeCode(line))
|
||||
}
|
||||
|
||||
predicate isDocumentation() {
|
||||
// If a C-style comment starts each line with a *, then it's
|
||||
// probably documentation rather than code.
|
||||
this instanceof CStyleComment and
|
||||
forex(int i | i in [1 .. this.numLines() - 1]
|
||||
| this.getLine(i).trim().matches("*%"))
|
||||
}
|
||||
predicate isDocumentation() {
|
||||
// If a C-style comment starts each line with a *, then it's
|
||||
// probably documentation rather than code.
|
||||
this instanceof CStyleComment and
|
||||
forex(int i | i in [1 .. this.numLines() - 1] | this.getLine(i).trim().matches("*%"))
|
||||
}
|
||||
|
||||
predicate isCommentedOutCode() {
|
||||
not this.isDocumentation() and
|
||||
this.numCodeLines().(float) / this.numLines().(float) > 0.5
|
||||
}
|
||||
predicate isCommentedOutCode() {
|
||||
not this.isDocumentation() and
|
||||
this.numCodeLines().(float) / this.numLines().(float) > 0.5
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
|
||||
*/
|
||||
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
|
||||
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _) and
|
||||
this.lastComment().getLocation().hasLocationInfo(_, _, _, 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
|
||||
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
|
||||
*/
|
||||
predicate hasLocationInfo(
|
||||
string filepath, int startline, int startcolumn, int endline, int endcolumn
|
||||
) {
|
||||
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, _, _) and
|
||||
this.lastComment().getLocation().hasLocationInfo(_, _, _, endline, endcolumn)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A piece of commented-out code, identified using heuristics
|
||||
*/
|
||||
class CommentedOutCode extends CommentBlock {
|
||||
CommentedOutCode() {
|
||||
this.isCommentedOutCode()
|
||||
}
|
||||
CommentedOutCode() { this.isCommentedOutCode() }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user