-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Swift: extract ImportDecl and ModuleDecl #9772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7a7440a
3a97517
f9143f7
e575bab
227dad8
8addc06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,8 +41,8 @@ class TrapOutput { | |
|
||
template <typename... Args> | ||
void debug(const Args&... args) { | ||
out_ << "// DEBUG: "; | ||
(out_ << ... << args) << '\n'; | ||
out_ << "/* DEBUG:\n"; | ||
(out_ << ... << args) << "\n*/\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uh, I forgot to mention the debugging enhancements. There's also |
||
} | ||
|
||
private: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,16 +241,15 @@ std::variant<codeql::AccessorDecl, codeql::AccessorDeclsTrap> DeclVisitor::trans | |
|
||
std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl( | ||
const swift::SubscriptDecl& decl) { | ||
auto id = dispatcher_.assignNewLabel(decl, mangledName(decl)); | ||
if (!dispatcher_.shouldEmitDeclBody(decl)) { | ||
auto entry = createNamedEntry(decl); | ||
if (!entry) { | ||
return std::nullopt; | ||
} | ||
SubscriptDecl entry{id}; | ||
entry.element_type = dispatcher_.fetchLabel(decl.getElementInterfaceType()); | ||
entry->element_type = dispatcher_.fetchLabel(decl.getElementInterfaceType()); | ||
if (auto indices = decl.getIndices()) { | ||
entry.params = dispatcher_.fetchRepeatedLabels(*indices); | ||
entry->params = dispatcher_.fetchRepeatedLabels(*indices); | ||
} | ||
fillAbstractStorageDecl(decl, entry); | ||
fillAbstractStorageDecl(decl, *entry); | ||
return entry; | ||
} | ||
|
||
|
@@ -262,7 +261,32 @@ codeql::ExtensionDecl DeclVisitor::translateExtensionDecl(const swift::Extension | |
return entry; | ||
} | ||
|
||
codeql::ImportDecl DeclVisitor::translateImportDecl(const swift::ImportDecl& decl) { | ||
auto entry = dispatcher_.createEntry(decl); | ||
entry.is_exported = decl.isExported(); | ||
entry.module = dispatcher_.fetchLabel(decl.getModule()); | ||
entry.declarations = dispatcher_.fetchRepeatedLabels(decl.getDecls()); | ||
return entry; | ||
} | ||
|
||
std::optional<codeql::ModuleDecl> DeclVisitor::translateModuleDecl(const swift::ModuleDecl& decl) { | ||
auto entry = createNamedEntry(decl); | ||
if (!entry) { | ||
return std::nullopt; | ||
} | ||
entry->is_builtin_module = decl.isBuiltinModule(); | ||
entry->is_system_module = decl.isSystemModule(); | ||
fillTypeDecl(decl, *entry); | ||
return entry; | ||
} | ||
|
||
std::string DeclVisitor::mangledName(const swift::ValueDecl& decl) { | ||
// ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl` | ||
// TODO find a more unique string working also when different modules are compiled with the same | ||
// name | ||
if (decl.getKind() == swift::DeclKind::Module) { | ||
return static_cast<const swift::ModuleDecl&>(decl).getRealName().str().str(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is my understanding correct that this is triggered only from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, not only, because I also added explicitly the current module |
||
} | ||
// prefix adds a couple of special symbols, we don't necessary need them | ||
return mangler.mangleAnyDecl(&decl, /* prefix = */ false); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
| file://:0:0:0:0 | A | | ||
| file://:0:0:0:0 | B | | ||
| file://:0:0:0:0 | main | | ||
| file://:0:0:0:0 | partial_modules | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import swift | ||
|
||
from ModuleDecl decl | ||
where not decl.isBuiltinModule() and not decl.isSystemModule() | ||
select decl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there is also no harm to emit the file unconditionally? 🤔
(Just thinking out loud, no action needed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, my idea is that we would just unconditionally call
visitor.extract(file)
, which would contain the logic for the emission and caching like the rest of the entities.