Skip to content

Commit 8b126fe

Browse files
committed
Swift: extract MacroDecl
1 parent 803ed20 commit 8b126fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+706
-10
lines changed

swift/extractor/infra/SwiftDispatcher.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class SwiftDispatcher {
3636
const swift::TypeBase*,
3737
const swift::CapturedValue*,
3838
const swift::PoundAvailableInfo*,
39-
const swift::AvailabilitySpec*>;
39+
const swift::AvailabilitySpec*,
40+
const swift::MacroRoleAttr*>;
4041

4142
public:
4243
// all references and pointers passed as parameters to this constructor are supposed to outlive
@@ -334,6 +335,7 @@ class SwiftDispatcher {
334335
virtual void visit(const swift::TypeRepr* typeRepr, swift::Type type) = 0;
335336
virtual void visit(const swift::TypeBase* type) = 0;
336337
virtual void visit(const swift::CapturedValue* capture) = 0;
338+
virtual void visit(const swift::MacroRoleAttr* attr) = 0;
337339

338340
template <typename T>
339341
requires(!std::derived_from<T, swift::TypeRepr>) void visit(const T* e, swift::Type) { visit(e); }

swift/extractor/infra/SwiftLocationExtractor.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ concept HasStartAndEndLoc = requires(T e) {
2828
}
2929
&&!(HasSourceRange<T>);
3030

31+
template <typename T>
32+
concept HasLAndRParenLoc = requires(T e) {
33+
e.getLParenLoc();
34+
e.getRParenLoc();
35+
}
36+
&&!(HasSourceRange<T>)&&!(HasStartAndEndLoc<T>);
37+
3138
template <typename T>
3239
concept HasOneLoc = requires(T e) {
3340
e.getLoc();
@@ -50,6 +57,13 @@ swift::SourceRange getSourceRange(const HasStartAndEndLoc auto& locatable) {
5057
return {locatable.getStartLoc()};
5158
}
5259

60+
swift::SourceRange getSourceRange(const HasLAndRParenLoc auto& locatable) {
61+
if (locatable.getLParenLoc() && locatable.getRParenLoc()) {
62+
return {locatable.getLParenLoc(), locatable.getRParenLoc()};
63+
}
64+
return {locatable.getLParenLoc()};
65+
}
66+
5367
swift::SourceRange getSourceRange(const HasOneLoc auto& locatable) {
5468
return {locatable.getLoc()};
5569
}

swift/extractor/infra/SwiftTagTraits.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ MAP(swift::Decl, DeclTag)
228228
MAP(swift::FuncDecl, AccessorOrNamedFunctionTag)
229229
MAP_CONCRETE(swift::FuncDecl, NamedFunctionTag)
230230
MAP(swift::AccessorDecl, AccessorTag)
231-
MAP(swift::MacroDecl, void) // TODO (introduced in 5.8)
231+
MAP(swift::MacroDecl, MacroDeclTag)
232232
MAP(swift::EnumElementDecl, EnumElementDeclTag)
233233
MAP(swift::ExtensionDecl, ExtensionDeclTag)
234234
MAP(swift::TopLevelCodeDecl, TopLevelCodeDeclTag)
@@ -350,6 +350,7 @@ MAP(swift::AvailabilitySpec, AvailabilitySpecTag)
350350
MAP(swift::OtherPlatformAvailabilitySpec, OtherAvailabilitySpecTag)
351351

352352
MAP(swift::PoundAvailableInfo, AvailabilityInfoTag)
353+
MAP(swift::MacroRoleAttr, MacroRoleTag)
353354

354355
// clang-format on
355356
#undef MAP

swift/extractor/translators/DeclTranslator.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,32 @@ codeql::CapturedDecl DeclTranslator::translateCapturedValue(const swift::Capture
374374
entry.is_escaping = !capture.isNoEscape();
375375
return entry;
376376
}
377+
378+
codeql::MacroDecl DeclTranslator::translateMacroDecl(const swift::MacroDecl& decl) {
379+
auto entry = createEntry(decl);
380+
fillValueDecl(decl, entry);
381+
fillGenericContext(decl, entry);
382+
entry.name = constructName(decl.getName());
383+
for (auto attr : decl.getAttrs().getAttributes<swift::MacroRoleAttr>()) {
384+
entry.roles.emplace_back(dispatcher.fetchLabel(attr));
385+
}
386+
if (decl.getParameterList()) {
387+
for (auto param : *decl.getParameterList()) {
388+
entry.parameters.emplace_back(dispatcher.fetchLabel(param));
389+
}
390+
}
391+
return entry;
392+
}
393+
394+
codeql::MacroRole DeclTranslator::translateMacroRoleAttr(const swift::MacroRoleAttr& attr) {
395+
auto entry = dispatcher.createEntry(attr);
396+
entry.kind = static_cast<uint32_t>(attr.getMacroRole());
397+
entry.macro_syntax = static_cast<uint8_t>(attr.getMacroSyntax());
398+
entry.conformances = dispatcher.fetchRepeatedLabels(attr.getConformances());
399+
for (auto& declName : attr.getNames()) {
400+
entry.names.emplace_back(constructName(declName.getName()));
401+
}
402+
return entry;
403+
}
404+
377405
} // namespace codeql

swift/extractor/translators/DeclTranslator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class DeclTranslator : public AstTranslatorBase<DeclTranslator> {
4949
codeql::PoundDiagnosticDecl translatePoundDiagnosticDecl(const swift::PoundDiagnosticDecl& decl);
5050
codeql::MissingMemberDecl translateMissingMemberDecl(const swift::MissingMemberDecl& decl);
5151
codeql::CapturedDecl translateCapturedValue(const swift::CapturedValue& capture);
52+
codeql::MacroDecl translateMacroDecl(const swift::MacroDecl& decl);
53+
codeql::MacroRole translateMacroRoleAttr(const swift::MacroRoleAttr& attr);
5254

5355
private:
5456
void fillFunction(const swift::AbstractFunctionDecl& decl, codeql::Function& entry);

swift/extractor/translators/SwiftVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class SwiftVisitor : private SwiftDispatcher {
5353
declTranslator.translateAndEmit(*capture);
5454
}
5555

56+
void visit(const swift::MacroRoleAttr* attr) override { declTranslator.translateAndEmit(*attr); }
57+
5658
DeclTranslator declTranslator{*this};
5759
ExprTranslator exprTranslator{*this};
5860
StmtTranslator stmtTranslator{*this};

swift/extractor/translators/TranslatorBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ class AstTranslatorBase : private swift::ASTVisitor<CrtpSubclass>,
8989
dispatcher.emit(static_cast<CrtpSubclass*>(this)->translateCapturedValue(e));
9090
}
9191

92+
void translateAndEmit(const swift::MacroRoleAttr& attr) {
93+
dispatcher.emit(static_cast<CrtpSubclass*>(this)->translateMacroRoleAttr(attr));
94+
}
95+
9296
private:
9397
friend class swift::ASTVisitor<CrtpSubclass>;
9498

swift/ql/.generated.list

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)