File tree Expand file tree Collapse file tree 12 files changed +73
-0
lines changed Expand file tree Collapse file tree 12 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,10 @@ Location:
34
34
end_column : int
35
35
_pragma : qltest_skip
36
36
37
+ Comment :
38
+ _extends : Locatable
39
+ text : string
40
+
37
41
Type :
38
42
name : string
39
43
canonical_type : Type
Original file line number Diff line number Diff line change @@ -104,11 +104,26 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
104
104
trap.emit (unknownFileEntry);
105
105
trap.emit (unknownLocationEntry);
106
106
107
+ std::vector<swift::Token> comments;
108
+ if (primaryFile && primaryFile->getBufferID ().hasValue ()) {
109
+ auto & sourceManager = compiler.getSourceMgr ();
110
+ auto tokens = swift::tokenize (compiler.getInvocation ().getLangOptions (), sourceManager,
111
+ primaryFile->getBufferID ().getValue ());
112
+ for (auto & token : tokens) {
113
+ if (token.getKind () == swift::tok::comment) {
114
+ comments.push_back (token);
115
+ }
116
+ }
117
+ }
118
+
107
119
SwiftVisitor visitor (compiler.getSourceMgr (), trap, module , primaryFile);
108
120
auto topLevelDecls = getTopLevelDecls (module , primaryFile);
109
121
for (auto decl : topLevelDecls) {
110
122
visitor.extract (decl);
111
123
}
124
+ for (auto & comment : comments) {
125
+ visitor.extract (comment);
126
+ }
112
127
}
113
128
114
129
static std::unordered_set<std::string> collectInputFilenames (swift::CompilerInstance& compiler) {
Original file line number Diff line number Diff line change 3
3
#include < swift/AST/SourceFile.h>
4
4
#include < swift/Basic/SourceManager.h>
5
5
#include < llvm/Support/FileSystem.h>
6
+ #include < swift/Parse/Token.h>
6
7
7
8
#include " swift/extractor/trap/TrapLabelStore.h"
8
9
#include " swift/extractor/trap/TrapDomain.h"
@@ -242,6 +243,12 @@ class SwiftDispatcher {
242
243
return false ;
243
244
}
244
245
246
+ void emitComment (swift::Token& comment) {
247
+ CommentsTrap entry{trap.createLabel <CommentTag>(), comment.getRawText ().str ()};
248
+ trap.emit (entry);
249
+ attachLocation (comment.getRange ().getStart (), comment.getRange ().getEnd (), entry.id );
250
+ }
251
+
245
252
private:
246
253
void attachLocation (swift::SourceLoc start,
247
254
swift::SourceLoc end,
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ class SwiftVisitor : private SwiftDispatcher {
17
17
void extract (const T& entity) {
18
18
fetchLabel (entity);
19
19
}
20
+ void extract (swift::Token& comment) { emitComment (comment); }
20
21
21
22
private:
22
23
void visit (swift::Decl* decl) override { declVisitor.visit (decl); }
Original file line number Diff line number Diff line change 1
1
// generated by codegen/codegen.py
2
2
import codeql.swift.elements.AstNode
3
3
import codeql.swift.elements.Callable
4
+ import codeql.swift.elements.Comment
4
5
import codeql.swift.elements.Element
5
6
import codeql.swift.elements.File
6
7
import codeql.swift.elements.Locatable
Original file line number Diff line number Diff line change
1
+ private import codeql.swift.generated.Comment
2
+
3
+ class Comment extends CommentBase {
4
+ /** toString */
5
+ override string toString ( ) { result = getText ( ) }
6
+ }
Original file line number Diff line number Diff line change
1
+ // generated by codegen/codegen.py
2
+ import codeql.swift.elements.Locatable
3
+
4
+ class CommentBase extends @comment, Locatable {
5
+ override string getAPrimaryQlClass ( ) { result = "Comment" }
6
+
7
+ string getText ( ) { comments ( this , result ) }
8
+ }
Original file line number Diff line number Diff line change 35
35
@locatable =
36
36
@argument
37
37
| @ast_node
38
+ | @comment
38
39
| @condition_element
39
40
| @if_config_clause
40
41
;
@@ -54,6 +55,11 @@ locations(
54
55
int end_column: int ref
55
56
);
56
57
58
+ comments(
59
+ unique int id: @comment,
60
+ string text: string ref
61
+ );
62
+
57
63
@type =
58
64
@any_function_type
59
65
| @any_generic_type
Original file line number Diff line number Diff line change
1
+ | comments.swift:1:1:2:1 | // Single line comment\n |
2
+ | comments.swift:3:1:6:3 | /*\n Multiline\n comment\n*/ |
3
+ | comments.swift:8:1:9:1 | /// Single line doc comment\n |
4
+ | comments.swift:10:1:13:3 | /**\n Multiline\n doc comment\n*/ |
Original file line number Diff line number Diff line change
1
+ import swift
2
+
3
+ from Comment c
4
+ select c
You can’t perform that action at this time.
0 commit comments