|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "swift/extractor/trap/TrapArena.h" |
| 4 | +#include "swift/extractor/trap/TrapLabelStore.h" |
| 5 | +#include "swift/extractor/trap/generated/TrapClasses.h" |
| 6 | +#include "swift/extractor/SwiftTagTraits.h" |
| 7 | +#include <swift/AST/SourceFile.h> |
| 8 | +#include <swift/Basic/SourceManager.h> |
| 9 | +#include <llvm/Support/FileSystem.h> |
| 10 | + |
| 11 | +namespace codeql { |
| 12 | + |
| 13 | +namespace detail { |
| 14 | + |
| 15 | +// The following `getKindName`s are used within "TBD" TRAP entries to visually mark an AST node as |
| 16 | +// not properly emitted yet. |
| 17 | +// TODO: To be replaced with QL counterpart |
| 18 | +template <typename Parent, typename Kind> |
| 19 | +inline std::string getKindName(Kind kind) { |
| 20 | + return Parent::getKindName(kind).str(); |
| 21 | +} |
| 22 | + |
| 23 | +template <> |
| 24 | +inline std::string getKindName<swift::TypeBase, swift::TypeKind>(swift::TypeKind kind) { |
| 25 | + switch (kind) { |
| 26 | +#define TYPE(CLASS, PARENT) \ |
| 27 | + case swift::TypeKind::CLASS: \ |
| 28 | + return #CLASS; |
| 29 | +#include "swift/AST/TypeNodes.def" |
| 30 | + default: |
| 31 | + return "Unknown"; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +template <> |
| 36 | +std::string inline getKindName<swift::TypeRepr, swift::TypeReprKind>(swift::TypeReprKind kind) { |
| 37 | + switch (kind) { |
| 38 | +#define TYPEREPR(CLASS, PARENT) \ |
| 39 | + case swift::TypeReprKind::CLASS: \ |
| 40 | + return #CLASS; |
| 41 | +#include "swift/AST/TypeReprNodes.def" |
| 42 | + default: |
| 43 | + return "Unknown"; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace detail |
| 48 | + |
| 49 | +// The main reponsibilities of the SwiftDispatcher are as follows: |
| 50 | +// * redirect specific AST node emission to a corresponding visitor (statements, expressions, etc.) |
| 51 | +// * storing TRAP labels for emitted AST nodes (in the TrapLabelStore) to avoid re-emission |
| 52 | +// Since SwiftDispatcher sees all the AST nodes, it also attaches a location to every 'locatable' |
| 53 | +// node (AST nodes that are not types: declarations, statements, expressions, etc.). |
| 54 | +class SwiftDispatcher { |
| 55 | + public: |
| 56 | + // sourceManager, arena, and trap are supposed to outlive the SwiftDispatcher |
| 57 | + SwiftDispatcher(const swift::SourceManager& sourceManager, TrapArena& arena, TrapOutput& trap) |
| 58 | + : sourceManager{sourceManager}, arena{arena}, trap{trap} {} |
| 59 | + |
| 60 | + template <typename T> |
| 61 | + void extract(T* entity) { |
| 62 | + fetchLabel(entity); |
| 63 | + } |
| 64 | + |
| 65 | + private: |
| 66 | + // This method gives a TRAP label for already emitted AST node. |
| 67 | + // If the AST node was not emitted yet, then the emission is dispatched to a corresponding |
| 68 | + // visitor (see `visit(T *)` methods below). |
| 69 | + template <typename E> |
| 70 | + TrapLabel<ToTag<E>> fetchLabel(E* e) { |
| 71 | + // this is required so we avoid any recursive loop: a `fetchLabel` during the visit of `e` might |
| 72 | + // end up calling `fetchLabel` on `e` itself, so we want the visit of `e` to call `fetchLabel` |
| 73 | + // only after having called `assignNewLabel` on `e` |
| 74 | + assert(!waitingForNewLabel && "fetchLabel called before assignNewLabel"); |
| 75 | + if (auto l = store.get(e)) { |
| 76 | + return *l; |
| 77 | + } |
| 78 | + waitingForNewLabel = getCanonicalPointer(e); |
| 79 | + visit(e); |
| 80 | + if (auto l = store.get(e)) { |
| 81 | + if constexpr (!std::is_base_of_v<swift::TypeBase, E>) { |
| 82 | + attachLocation(e, *l); |
| 83 | + } |
| 84 | + return *l; |
| 85 | + } |
| 86 | + assert(!"assignNewLabel not called during visit"); |
| 87 | + return {}; |
| 88 | + } |
| 89 | + |
| 90 | + // Due to the lazy emission approach, we must assign a label to a corresponding AST node before |
| 91 | + // it actually gets emitted to handle recursive cases such as recursive calls, or recursive type |
| 92 | + // declarations |
| 93 | + template <typename E> |
| 94 | + TrapLabel<ToTag<E>> assignNewLabel(E* e) { |
| 95 | + assert(waitingForNewLabel == getCanonicalPointer(e) && "assignNewLabel called on wrong entity"); |
| 96 | + auto label = getLabel<ToTag<E>>(); |
| 97 | + trap.assignStar(label); |
| 98 | + store.insert(e, label); |
| 99 | + waitingForNewLabel = nullptr; |
| 100 | + return label; |
| 101 | + } |
| 102 | + |
| 103 | + template <typename Tag> |
| 104 | + TrapLabel<Tag> getLabel() { |
| 105 | + return arena.allocateLabel<Tag>(); |
| 106 | + } |
| 107 | + |
| 108 | + // This is a helper method to emit TRAP entries for AST nodes that we don't fully support yet. |
| 109 | + template <typename Parent, typename Child> |
| 110 | + void TBD(Child* entity, const std::string& suffix) { |
| 111 | + using namespace std::string_literals; |
| 112 | + auto label = assignNewLabel(entity); |
| 113 | + auto kind = detail::getKindName<Parent>(static_cast<const Parent*>(entity)->getKind()); |
| 114 | + auto name = "TBD ("s + kind + suffix + ")"; |
| 115 | + if constexpr (std::is_same_v<Parent, swift::TypeBase>) { |
| 116 | + trap.emit(UnknownTypesTrap{label, name}); |
| 117 | + } else { |
| 118 | + trap.emit(UnknownAstNodesTrap{label, name}); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + template <typename Locatable> |
| 123 | + void attachLocation(Locatable locatable, TrapLabel<LocatableTag> locatableLabel) { |
| 124 | + attachLocation(&locatable, locatableLabel); |
| 125 | + } |
| 126 | + |
| 127 | + // Emits a Location TRAP entry and attaches it to an AST node |
| 128 | + template <typename Locatable> |
| 129 | + void attachLocation(Locatable* locatable, TrapLabel<LocatableTag> locatableLabel) { |
| 130 | + auto start = locatable->getStartLoc(); |
| 131 | + auto end = locatable->getEndLoc(); |
| 132 | + if (!start.isValid() || !end.isValid()) { |
| 133 | + // invalid locations seem to come from entities synthesized by the compiler |
| 134 | + return; |
| 135 | + } |
| 136 | + std::string filepath = getFilepath(start); |
| 137 | + auto fileLabel = arena.allocateLabel<FileTag>(); |
| 138 | + trap.assignKey(fileLabel, filepath); |
| 139 | + // TODO: do not emit duplicate trap entries for Files |
| 140 | + trap.emit(FilesTrap{fileLabel, filepath}); |
| 141 | + auto [startLine, startColumn] = sourceManager.getLineAndColumnInBuffer(start); |
| 142 | + auto [endLine, endColumn] = sourceManager.getLineAndColumnInBuffer(end); |
| 143 | + auto locLabel = arena.allocateLabel<LocationTag>(); |
| 144 | + trap.assignKey(locLabel, '{', fileLabel, "}:", startLine, ':', startColumn, ':', endLine, ':', |
| 145 | + endColumn); |
| 146 | + trap.emit(LocationsTrap{locLabel, fileLabel, startLine, startColumn, endLine, endColumn}); |
| 147 | + trap.emit(LocatablesTrap{locatableLabel, locLabel}); |
| 148 | + } |
| 149 | + |
| 150 | + std::string getFilepath(swift::SourceLoc loc) { |
| 151 | + // TODO: this needs more testing |
| 152 | + // TODO: check canonicaliztion of names on a case insensitive filesystems |
| 153 | + // TODO: make symlink resolution conditional on CODEQL_PRESERVE_SYMLINKS=true |
| 154 | + auto displayName = sourceManager.getDisplayNameForLoc(loc); |
| 155 | + llvm::SmallString<PATH_MAX> realPath; |
| 156 | + if (std::error_code ec = llvm::sys::fs::real_path(displayName, realPath)) { |
| 157 | + std::cerr << "Cannot get real path: '" << displayName.str() << "': " << ec.message() << "\n"; |
| 158 | + return {}; |
| 159 | + } |
| 160 | + return realPath.str().str(); |
| 161 | + } |
| 162 | + |
| 163 | + // TODO: The following methods are supposed to redirect TRAP emission to correpsonding visitors, |
| 164 | + // which are to be introduced in follow-up PRs |
| 165 | + void visit(swift::Decl* decl) { TBD<swift::Decl>(decl, "Decl"); } |
| 166 | + void visit(swift::Stmt* stmt) { TBD<swift::Stmt>(stmt, "Stmt"); } |
| 167 | + void visit(swift::Expr* expr) { TBD<swift::Expr>(expr, "Expr"); } |
| 168 | + void visit(swift::Pattern* pattern) { TBD<swift::Pattern>(pattern, "Pattern"); } |
| 169 | + void visit(swift::TypeRepr* type) { TBD<swift::TypeRepr>(type, "TypeRepr"); } |
| 170 | + void visit(swift::TypeBase* type) { TBD<swift::TypeBase>(type, "Type"); } |
| 171 | + |
| 172 | + const swift::SourceManager& sourceManager; |
| 173 | + TrapArena& arena; |
| 174 | + TrapOutput& trap; |
| 175 | + TrapLabelStore store; |
| 176 | + const void* waitingForNewLabel{nullptr}; |
| 177 | +}; |
| 178 | + |
| 179 | +} // namespace codeql |
0 commit comments