|
9 | 9 | #include "clang/Frontend/FrontendAction.h"
|
10 | 10 | #include "clang/AST/ASTConsumer.h"
|
11 | 11 | #include "clang/AST/ASTContext.h"
|
| 12 | +#include "clang/AST/Decl.h" |
12 | 13 | #include "clang/AST/DeclGroup.h"
|
13 | 14 | #include "clang/Basic/Builtins.h"
|
14 | 15 | #include "clang/Basic/DiagnosticOptions.h"
|
|
39 | 40 | #include "clang/Serialization/ASTReader.h"
|
40 | 41 | #include "clang/Serialization/GlobalModuleIndex.h"
|
41 | 42 | #include "llvm/ADT/ScopeExit.h"
|
| 43 | +#include "llvm/ADT/SmallPtrSet.h" |
42 | 44 | #include "llvm/ADT/StringRef.h"
|
43 | 45 | #include "llvm/Support/BuryPointer.h"
|
44 | 46 | #include "llvm/Support/ErrorHandling.h"
|
@@ -87,12 +89,25 @@ class DeserializedDeclsSourceRangePrinter : public ASTConsumer,
|
87 | 89 | // reducing the granularity and making the output less useful.
|
88 | 90 | return;
|
89 | 91 | }
|
90 |
| - if (auto *DC = D->getDeclContext(); !DC || !DC->isFileContext()) { |
| 92 | + auto *DC = D->getLexicalDeclContext(); |
| 93 | + if (!DC || !DC->isFileContext()) { |
91 | 94 | // We choose to work at namespace level to reduce complexity and the
|
92 | 95 | // number of cases we care about.
|
93 | 96 | return;
|
94 | 97 | }
|
| 98 | + |
95 | 99 | PendingDecls.push_back(D);
|
| 100 | + if (auto *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 101 | + // Add any namespaces we have not seen before. |
| 102 | + // Note that we filter out namespaces from DeclRead as it includes too |
| 103 | + // all redeclarations and we only want the ones that had other used |
| 104 | + // declarations. |
| 105 | + while (NS && ProcessedNamespaces.insert(NS).second) { |
| 106 | + PendingDecls.push_back(NS); |
| 107 | + |
| 108 | + NS = dyn_cast<NamespaceDecl>(NS->getLexicalParent()); |
| 109 | + } |
| 110 | + } |
96 | 111 | }
|
97 | 112 |
|
98 | 113 | struct Position {
|
@@ -141,23 +156,25 @@ class DeserializedDeclsSourceRangePrinter : public ASTConsumer,
|
141 | 156 | OptionalFileEntryRef Ref;
|
142 | 157 | };
|
143 | 158 | llvm::DenseMap<const FileEntry *, FileData> FileToRanges;
|
| 159 | + |
144 | 160 | for (const Decl *D : PendingDecls) {
|
145 |
| - CharSourceRange R = SM.getExpansionRange(D->getSourceRange()); |
146 |
| - if (!R.isValid()) |
147 |
| - continue; |
| 161 | + for (CharSourceRange R : getRangesToMark(D)) { |
| 162 | + if (!R.isValid()) |
| 163 | + continue; |
148 | 164 |
|
149 |
| - auto *F = SM.getFileEntryForID(SM.getFileID(R.getBegin())); |
150 |
| - if (F != SM.getFileEntryForID(SM.getFileID(R.getEnd()))) { |
151 |
| - // Such cases are rare and difficult to handle. |
152 |
| - continue; |
153 |
| - } |
| 165 | + auto *F = SM.getFileEntryForID(SM.getFileID(R.getBegin())); |
| 166 | + if (F != SM.getFileEntryForID(SM.getFileID(R.getEnd()))) { |
| 167 | + // Such cases are rare and difficult to handle. |
| 168 | + continue; |
| 169 | + } |
154 | 170 |
|
155 |
| - auto &Data = FileToRanges[F]; |
156 |
| - if (!Data.Ref) |
157 |
| - Data.Ref = SM.getFileEntryRefForID(SM.getFileID(R.getBegin())); |
158 |
| - Data.FromTo.push_back( |
159 |
| - {Position::GetBeginSpelling(SM, R), |
160 |
| - Position::GetEndSpelling(SM, R, D->getLangOpts())}); |
| 171 | + auto &Data = FileToRanges[F]; |
| 172 | + if (!Data.Ref) |
| 173 | + Data.Ref = SM.getFileEntryRefForID(SM.getFileID(R.getBegin())); |
| 174 | + Data.FromTo.push_back( |
| 175 | + {Position::GetBeginSpelling(SM, R), |
| 176 | + Position::GetEndSpelling(SM, R, D->getLangOpts())}); |
| 177 | + } |
161 | 178 | }
|
162 | 179 |
|
163 | 180 | // To simplify output, merge consecutive and intersecting ranges.
|
@@ -188,10 +205,49 @@ class DeserializedDeclsSourceRangePrinter : public ASTConsumer,
|
188 | 205 |
|
189 | 206 | private:
|
190 | 207 | std::vector<const Decl *> PendingDecls;
|
| 208 | + llvm::SmallPtrSet<const NamespaceDecl *, 0> ProcessedNamespaces; |
191 | 209 | bool IsCollectingDecls = true;
|
192 | 210 | const SourceManager &SM;
|
193 | 211 | std::unique_ptr<llvm::raw_ostream> OS;
|
194 | 212 |
|
| 213 | + llvm::SmallVector<CharSourceRange, 2> getRangesToMark(const Decl *D) { |
| 214 | + auto *NS = dyn_cast<NamespaceDecl>(D); |
| 215 | + if (!NS) |
| 216 | + return {SM.getExpansionRange(D->getSourceRange())}; |
| 217 | + |
| 218 | + SourceLocation LBraceLoc; |
| 219 | + if (NS->isAnonymousNamespace()) { |
| 220 | + LBraceLoc = NS->getLocation(); |
| 221 | + } else { |
| 222 | + // Start with the location of the identifier. |
| 223 | + SourceLocation TokenBeforeLBrace = NS->getLocation(); |
| 224 | + if (NS->hasAttrs()) { |
| 225 | + for (auto *A : NS->getAttrs()) { |
| 226 | + // But attributes may go after it. |
| 227 | + if (SM.isBeforeInTranslationUnit(TokenBeforeLBrace, |
| 228 | + A->getRange().getEnd())) { |
| 229 | + // Give up, the attributes are often coming from macros and we |
| 230 | + // cannot skip them reliably. |
| 231 | + return {}; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + auto &LangOpts = D->getLangOpts(); |
| 236 | + // Now skip one token, the next should be the lbrace. |
| 237 | + Token Tok; |
| 238 | + if (Lexer::getRawToken(TokenBeforeLBrace, Tok, SM, LangOpts, true) || |
| 239 | + Lexer::getRawToken(Tok.getEndLoc(), Tok, SM, LangOpts, true) || |
| 240 | + Tok.getKind() != tok::l_brace) { |
| 241 | + // On error or if we did not find the token we expected, avoid marking |
| 242 | + // everything inside the namespace as used. |
| 243 | + return {}; |
| 244 | + } |
| 245 | + LBraceLoc = Tok.getLocation(); |
| 246 | + } |
| 247 | + return {SM.getExpansionRange(SourceRange(NS->getBeginLoc(), LBraceLoc)), |
| 248 | + SM.getExpansionRange(NS->getRBraceLoc())}; |
| 249 | + } |
| 250 | + |
195 | 251 | void printJson(llvm::ArrayRef<RequiredRanges> Result) {
|
196 | 252 | *OS << "{\n";
|
197 | 253 | *OS << R"( "required_ranges": [)" << "\n";
|
|
0 commit comments