Skip to content

Commit 5e69adb

Browse files
committed
Swift: extract comments
1 parent 1c8090f commit 5e69adb

File tree

12 files changed

+73
-0
lines changed

12 files changed

+73
-0
lines changed

swift/codegen/schema.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Location:
3434
end_column: int
3535
_pragma: qltest_skip
3636

37+
Comment:
38+
_extends: Locatable
39+
text: string
40+
3741
Type:
3842
name: string
3943
canonical_type: Type

swift/extractor/SwiftExtractor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,26 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
104104
trap.emit(unknownFileEntry);
105105
trap.emit(unknownLocationEntry);
106106

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+
107119
SwiftVisitor visitor(compiler.getSourceMgr(), trap, module, primaryFile);
108120
auto topLevelDecls = getTopLevelDecls(module, primaryFile);
109121
for (auto decl : topLevelDecls) {
110122
visitor.extract(decl);
111123
}
124+
for (auto& comment : comments) {
125+
visitor.extract(comment);
126+
}
112127
}
113128

114129
static std::unordered_set<std::string> collectInputFilenames(swift::CompilerInstance& compiler) {

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <swift/AST/SourceFile.h>
44
#include <swift/Basic/SourceManager.h>
55
#include <llvm/Support/FileSystem.h>
6+
#include <swift/Parse/Token.h>
67

78
#include "swift/extractor/trap/TrapLabelStore.h"
89
#include "swift/extractor/trap/TrapDomain.h"
@@ -242,6 +243,12 @@ class SwiftDispatcher {
242243
return false;
243244
}
244245

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+
245252
private:
246253
void attachLocation(swift::SourceLoc start,
247254
swift::SourceLoc end,

swift/extractor/visitors/SwiftVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class SwiftVisitor : private SwiftDispatcher {
1717
void extract(const T& entity) {
1818
fetchLabel(entity);
1919
}
20+
void extract(swift::Token& comment) { emitComment(comment); }
2021

2122
private:
2223
void visit(swift::Decl* decl) override { declVisitor.visit(decl); }

swift/ql/lib/codeql/swift/elements.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// generated by codegen/codegen.py
22
import codeql.swift.elements.AstNode
33
import codeql.swift.elements.Callable
4+
import codeql.swift.elements.Comment
45
import codeql.swift.elements.Element
56
import codeql.swift.elements.File
67
import codeql.swift.elements.Locatable
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
private import codeql.swift.generated.Comment
2+
3+
class Comment extends CommentBase {
4+
/** toString */
5+
override string toString() { result = getText() }
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

swift/ql/lib/swift.dbscheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ files(
3535
@locatable =
3636
@argument
3737
| @ast_node
38+
| @comment
3839
| @condition_element
3940
| @if_config_clause
4041
;
@@ -54,6 +55,11 @@ locations(
5455
int end_column: int ref
5556
);
5657

58+
comments(
59+
unique int id: @comment,
60+
string text: string ref
61+
);
62+
5763
@type =
5864
@any_function_type
5965
| @any_generic_type
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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*/ |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import swift
2+
3+
from Comment c
4+
select c

0 commit comments

Comments
 (0)