Skip to content

Commit 1eec067

Browse files
authored
Merge pull request #11294 from erik-krogh/fileDoc
QL: improve the "this block-comment should have been a QLDoc"-query
2 parents 3d4f64f + de2ebe3 commit 1eec067

File tree

29 files changed

+123
-36
lines changed

29 files changed

+123
-36
lines changed

cpp/ql/lib/semmle/code/cpp/security/TaintTracking.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Support for tracking tainted data through the program. This is an alias for
33
* `semmle.code.cpp.ir.dataflow.DefaultTaintTracking` provided for backwards
44
* compatibility.

cpp/ql/src/jsf/4.09 Style/Naming.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Common functions for implementing naming conventions
33
*
44
* Naming rules are the following:

csharp/ql/campaigns/Solorigate/lib/Solorigate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Provides reusable predicates related to Solorigate
33
*/
44

csharp/ql/lib/experimental/code/csharp/Cryptography/NonCryptographicHashes.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Predicates that help detect potential non-cryptographic hash functions
33
*
44
* By themselves, non-cryptographic functions are common and not dangerous

java/ql/lib/semmle/code/java/frameworks/Properties.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Definitions related to `java.util.Properties`. */
1+
/** Definitions related to `java.util.Properties`. */
2+
23
import semmle.code.java.Type
34
private import semmle.code.java.dataflow.FlowSteps
45

java/ql/lib/semmle/code/java/frameworks/Rmi.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Remote Method Invocation. */
1+
/** Remote Method Invocation. */
2+
23
import java
34

45
/** The interface `java.rmi.Remote`. */

java/ql/lib/semmle/code/java/frameworks/apache/Exec.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Definitions related to the Apache Commons Exec library. */
1+
/** Definitions related to the Apache Commons Exec library. */
2+
23
import semmle.code.java.Type
34
import semmle.code.java.security.ExternalProcess
45

java/ql/lib/semmle/code/java/security/ExternalProcess.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Definitions related to external processes. */
1+
/** Definitions related to external processes. */
2+
23
import semmle.code.java.Member
34

45
private module Instances {

java/ql/lib/semmle/code/java/security/RelativePaths.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Detection of strings and arrays of strings containing relative paths. */
1+
/** Detection of strings and arrays of strings containing relative paths. */
2+
23
import java
34

45
/**

java/ql/lib/semmle/code/java/security/SqlUnescapedLib.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Definitions used by `SqlUnescaped.ql`. */
1+
/** Definitions used by `SqlUnescaped.ql`. */
2+
23
import semmle.code.java.security.ControlledString
34
import semmle.code.java.dataflow.TaintTracking
45

java/ql/src/Violations of Best Practice/Dead Code/DeadLocals.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* Provides classes and predicates for "dead locals": which variables are used, which assignments are useless, etc.
33
*/
44

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/ATMConfig.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Configures boosting for adaptive threat modeling (ATM).

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/AdaptiveThreatModeling.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Provides information about the results of boosted queries for use in adaptive threat modeling (ATM).

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/BaseScoring.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Provides shared scoring functionality for use in adaptive threat modeling (ATM).

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/CoreKnowledge.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Provides predicates that expose the knowledge of models

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointFeatures.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Extracts data about the database for use in adaptive threat modeling (ATM).

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/EndpointScoring.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Provides an implementation of scoring alerts for use in adaptive threat modeling (ATM).

javascript/ql/experimental/adaptivethreatmodeling/lib/experimental/adaptivethreatmodeling/FunctionBodyFeatures.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* FunctionBodyFeatures.qll
33
*
44
* Contains logic relating to the `enclosingFunctionBody` and `enclosingFunctionName` features.

javascript/ql/experimental/adaptivethreatmodeling/modelbuilding/counting/CountAlertsAndSinks.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
*

javascript/ql/experimental/adaptivethreatmodeling/modelbuilding/extraction/Exclusions.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Defines files that should be excluded from the evaluation of ML models.

javascript/ql/experimental/adaptivethreatmodeling/modelbuilding/extraction/ExtractEndpointDataTraining.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* For internal use only.
33
*
44
* Extracts training data we can use to train ML models for ML-powered queries.

javascript/ql/test/tutorials/Validating RAML-based APIs/Osprey.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Model of Osprey API implementations. */
1+
/** Model of Osprey API implementations. */
2+
23
import javascript
34
import HTTP
45

javascript/ql/test/tutorials/Validating RAML-based APIs/RAML.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Model of RAML specifications. */
1+
/** Model of RAML specifications. */
2+
23
import javascript
34
import HTTP
45

python/ql/lib/semmle/python/TestUtils.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* This file contains test-related utility functions */
1+
/** This file contains test-related utility functions */
2+
23
import python
34

45
/** Removes everything up to the occurrence of `sub` in the string `str` */

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,24 @@ class TopLevel extends TTopLevel, AstNode {
156156
}
157157

158158
QLDoc getQLDocFor(ModuleMember m) {
159-
exists(int i | i > 0 and result = this.getMember(i) and m = this.getMember(i + 1))
159+
exists(int i | result = this.getMember(i) and m = this.getMember(i + 1)) and
160+
(
161+
m instanceof ClasslessPredicate
162+
or
163+
m instanceof Class
164+
or
165+
m instanceof Module
166+
)
160167
}
161168

162169
override string getAPrimaryQlClass() { result = "TopLevel" }
163170

164-
override QLDoc getQLDoc() { result = this.getMember(0) }
171+
override QLDoc getQLDoc() {
172+
result = this.getMember(0) and
173+
// it's not the QLDoc for a module member
174+
not this.getQLDocFor(_) = result and
175+
result.getLocation().getStartLine() = 1 // this might not hold if there is a block comment above, and that's the point.
176+
}
165177
}
166178

167179
abstract class Comment extends AstNode, TComment {
@@ -536,6 +548,12 @@ class ClasslessPredicate extends TClasslessPredicate, Predicate, ModuleDeclarati
536548

537549
/** Holds if this classless predicate is a signature predicate with no body. */
538550
predicate isSignature() { not exists(this.getBody()) }
551+
552+
override QLDoc getQLDoc() {
553+
result = any(TopLevel m).getQLDocFor(this)
554+
or
555+
result = any(Module m).getQLDocFor(this)
556+
}
539557
}
540558

541559
/**

ql/ql/src/queries/style/NonDocBlock.ql

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,59 @@ int getLineAboveNodeThatCouldHaveDoc(File file) {
2929

3030
pragma[noinline]
3131
BlockComment getACommentThatCouldBeQLDoc(File file) {
32-
file = result.getLocation().getFile() and
33-
result.getLocation().getEndLine() = getLineAboveNodeThatCouldHaveDoc(file) and
34-
result.getLocation().getFile().getExtension() = "qll" and
35-
not result.getContents().matches("/**%")
32+
exists(Location loc | loc = result.getLocation() |
33+
file = loc.getFile() and
34+
loc.getFile().getExtension() = "qll" and
35+
not result.getContents().matches("/**%") and
36+
not [loc.getStartLine(), loc.getEndLine()] = getLinesWithNonComment(file) and
37+
(
38+
// above something that can be commented.
39+
loc.getEndLine() = getLineAboveNodeThatCouldHaveDoc(file)
40+
or
41+
// toplevel in file.
42+
loc.getStartLine() = 1 and
43+
loc.getStartColumn() = 1
44+
)
45+
)
46+
}
47+
48+
pragma[noinline]
49+
int getLinesWithNonComment(File f) {
50+
exists(AstNode n, Location loc |
51+
not n instanceof Comment and
52+
not n instanceof TopLevel and
53+
loc = n.getLocation() and
54+
loc.getFile() = f
55+
|
56+
result = [loc.getEndLine(), loc.getStartLine()]
57+
)
3658
}
3759

3860
pragma[noinline]
39-
BlockComment getCommentAt(File file, int endLine) {
61+
BlockComment getCommentAtEnd(File file, int endLine) {
4062
result = getACommentThatCouldBeQLDoc(file) and
4163
result.getLocation().getEndLine() = endLine
4264
}
4365

44-
from AstNode node, BlockComment comment
66+
pragma[noinline]
67+
BlockComment getCommentAtStart(File file, int startLine) {
68+
result = getACommentThatCouldBeQLDoc(file) and
69+
result.getLocation().getStartLine() = startLine
70+
}
71+
72+
from AstNode node, BlockComment comment, string nodeDescrip
4573
where
46-
canHaveQLDoc(node) and
74+
(
75+
canHaveQLDoc(node) and
76+
comment = getCommentAtEnd(node.getLocation().getFile(), node.getLocation().getStartLine() - 1) and
77+
nodeDescrip = "the below code"
78+
or
79+
node instanceof TopLevel and
80+
comment = getCommentAtStart(node.getLocation().getFile(), 1) and
81+
nodeDescrip = "the file"
82+
) and
4783
not exists(node.getQLDoc()) and
4884
not node.(ClassPredicate).isOverride() and // ignore override predicates
4985
not node.hasAnnotation("deprecated") and // ignore deprecated
50-
not node.hasAnnotation("private") and // ignore private
51-
comment = getCommentAt(node.getLocation().getFile(), node.getLocation().getStartLine() - 1)
52-
select comment, "Block comment could be QLDoc for $@.", node, "the below code"
86+
not node.hasAnnotation("private") // ignore private
87+
select comment, "Block comment could be QLDoc for $@.", node, nodeDescrip
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* This should be QLDoc.
3+
*/
4+
5+
/**
6+
* this is fine
7+
*/
8+
predicate foo() { any() }
9+
10+
/* Note: this is bad. */
11+
class Foo extends string {
12+
Foo() { this = "FOo" }
13+
}
14+
15+
/**
16+
* This is also fine.
17+
*/
18+
/*abstract*/ class Bar extends string {
19+
string getMergeRaw() { none() } // <- fine. The abstract comment is fine, it doesn't need to be QLDoc.
20+
21+
Bar() { this = "bar" }
22+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Foo.qll:1:1:3:3 | BlockComment | Block comment could be QLDoc for $@. | Foo.qll:1:1:22:2 | TopLevel | the file |
2+
| Foo.qll:10:1:10:24 | BlockComment | Block comment could be QLDoc for $@. | Foo.qll:11:7:11:9 | Class Foo | the below code |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/NonDocBlock.ql

0 commit comments

Comments
 (0)