aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/FrontendAction.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang/lib/Frontend/FrontendAction.cpp')
-rw-r--r--clang/lib/Frontend/FrontendAction.cpp116
1 files changed, 72 insertions, 44 deletions
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index 87cc2fc..a7d6a068 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -84,29 +84,28 @@ public:
if (!IsCollectingDecls)
return;
if (!D || isa<TranslationUnitDecl>(D) || isa<LinkageSpecDecl>(D) ||
- isa<NamespaceDecl>(D)) {
+ isa<NamespaceDecl>(D) || isa<ExportDecl>(D)) {
// These decls cover a lot of nested declarations that might not be used,
// reducing the granularity and making the output less useful.
return;
}
- auto *DC = D->getLexicalDeclContext();
- if (!DC || !DC->isFileContext()) {
- // We choose to work at namespace level to reduce complexity and the
- // number of cases we care about.
+ if (isa<ParmVarDecl>(D)) {
+ // Parameters are covered by their functions.
return;
}
+ auto *DC = D->getLexicalDeclContext();
+ if (!DC || !shouldIncludeDeclsIn(DC))
+ return;
PendingDecls.push_back(D);
- if (auto *NS = dyn_cast<NamespaceDecl>(DC)) {
- // Add any namespaces we have not seen before.
- // Note that we filter out namespaces from DeclRead as it includes too
- // all redeclarations and we only want the ones that had other used
- // declarations.
- while (NS && ProcessedNamespaces.insert(NS).second) {
- PendingDecls.push_back(NS);
-
- NS = dyn_cast<NamespaceDecl>(NS->getLexicalParent());
- }
+ for (; (isa<ExportDecl>(DC) || isa<NamespaceDecl>(DC)) &&
+ ProcessedDeclContexts.insert(DC).second;
+ DC = DC->getLexicalParent()) {
+ // Add any interesting decl contexts that we have not seen before.
+ // Note that we filter them out from DeclRead as that would include all
+ // redeclarations of namespaces, potentially those that do not have any
+ // imported declarations.
+ PendingDecls.push_back(cast<Decl>(DC));
}
}
@@ -205,12 +204,38 @@ public:
private:
std::vector<const Decl *> PendingDecls;
- llvm::SmallPtrSet<const NamespaceDecl *, 0> ProcessedNamespaces;
+ llvm::SmallPtrSet<const DeclContext *, 0> ProcessedDeclContexts;
bool IsCollectingDecls = true;
const SourceManager &SM;
std::unique_ptr<llvm::raw_ostream> OS;
+ static bool shouldIncludeDeclsIn(const DeclContext *DC) {
+ assert(DC && "DC is null");
+ // We choose to work at namespace level to reduce complexity and the number
+ // of cases we care about.
+ // We still need to carefully handle composite declarations like
+ // `ExportDecl`.
+ for (; DC; DC = DC->getLexicalParent()) {
+ if (DC->isFileContext())
+ return true;
+ if (isa<ExportDecl>(DC))
+ continue; // Depends on the parent.
+ return false;
+ }
+ llvm_unreachable("DeclContext chain must end with a translation unit");
+ }
+
llvm::SmallVector<CharSourceRange, 2> getRangesToMark(const Decl *D) {
+ if (auto *ED = dyn_cast<ExportDecl>(D)) {
+ if (!ED->hasBraces())
+ return {SM.getExpansionRange(ED->getExportLoc())};
+
+ return {SM.getExpansionRange(SourceRange(
+ ED->getExportLoc(),
+ lexForLBrace(ED->getExportLoc(), D->getLangOpts()))),
+ SM.getExpansionRange(ED->getRBraceLoc())};
+ }
+
auto *NS = dyn_cast<NamespaceDecl>(D);
if (!NS)
return {SM.getExpansionRange(D->getSourceRange())};
@@ -232,17 +257,7 @@ private:
}
}
}
- auto &LangOpts = D->getLangOpts();
- // Now skip one token, the next should be the lbrace.
- Token Tok;
- if (Lexer::getRawToken(TokenBeforeLBrace, Tok, SM, LangOpts, true) ||
- Lexer::getRawToken(Tok.getEndLoc(), Tok, SM, LangOpts, true) ||
- Tok.getKind() != tok::l_brace) {
- // On error or if we did not find the token we expected, avoid marking
- // everything inside the namespace as used.
- return {};
- }
- LBraceLoc = Tok.getLocation();
+ LBraceLoc = lexForLBrace(TokenBeforeLBrace, D->getLangOpts());
}
return {SM.getExpansionRange(SourceRange(NS->getBeginLoc(), LBraceLoc)),
SM.getExpansionRange(NS->getRBraceLoc())};
@@ -285,6 +300,20 @@ private:
OS->flush();
}
+
+ SourceLocation lexForLBrace(SourceLocation TokenBeforeLBrace,
+ const LangOptions &LangOpts) {
+ // Now skip one token, the next should be the lbrace.
+ Token Tok;
+ if (Lexer::getRawToken(TokenBeforeLBrace, Tok, SM, LangOpts, true) ||
+ Lexer::getRawToken(Tok.getEndLoc(), Tok, SM, LangOpts, true) ||
+ Tok.getKind() != tok::l_brace) {
+ // On error or if we did not find the token we expected, avoid marking
+ // everything inside the namespace as used.
+ return SourceLocation();
+ }
+ return Tok.getLocation();
+ }
};
/// Dumps deserialized declarations.
@@ -845,7 +874,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
// Set the shared objects, these are reset when we finish processing the
// file, otherwise the CompilerInstance will happily destroy them.
- CI.setFileManager(&AST->getFileManager());
+ CI.setFileManager(AST->getFileManagerPtr());
CI.createSourceManager(CI.getFileManager());
CI.getSourceManager().initializeForReplay(AST->getSourceManager());
@@ -912,13 +941,13 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
// Set the shared objects, these are reset when we finish processing the
// file, otherwise the CompilerInstance will happily destroy them.
- CI.setFileManager(&AST->getFileManager());
- CI.setSourceManager(&AST->getSourceManager());
+ CI.setFileManager(AST->getFileManagerPtr());
+ CI.setSourceManager(AST->getSourceManagerPtr());
CI.setPreprocessor(AST->getPreprocessorPtr());
Preprocessor &PP = CI.getPreprocessor();
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
PP.getLangOpts());
- CI.setASTContext(&AST->getASTContext());
+ CI.setASTContext(AST->getASTContextPtr());
setCurrentInput(Input, std::move(AST));
@@ -1172,11 +1201,12 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
// Convert headers to PCH and chain them.
- IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
+ IntrusiveRefCntPtr<ExternalSemaSource> source;
+ IntrusiveRefCntPtr<ASTReader> FinalReader;
source = createChainedIncludesSource(CI, FinalReader);
if (!source)
return false;
- CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
+ CI.setASTReader(FinalReader);
CI.getASTContext().setExternalSource(source);
} else if (CI.getLangOpts().Modules ||
!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
@@ -1252,23 +1282,21 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
// provides the layouts from that file.
if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
- IntrusiveRefCntPtr<ExternalASTSource>
- Override(new LayoutOverrideSource(
- CI.getFrontendOpts().OverrideRecordLayoutsFile));
+ auto Override = llvm::makeIntrusiveRefCnt<LayoutOverrideSource>(
+ CI.getFrontendOpts().OverrideRecordLayoutsFile);
CI.getASTContext().setExternalSource(Override);
}
// Setup HLSL External Sema Source
if (CI.getLangOpts().HLSL && CI.hasASTContext()) {
- IntrusiveRefCntPtr<ExternalSemaSource> HLSLSema(
- new HLSLExternalSemaSource());
- if (auto *SemaSource = dyn_cast_if_present<ExternalSemaSource>(
- CI.getASTContext().getExternalSource())) {
- IntrusiveRefCntPtr<ExternalSemaSource> MultiSema(
- new MultiplexExternalSemaSource(SemaSource, HLSLSema.get()));
- CI.getASTContext().setExternalSource(MultiSema);
+ auto HLSLSema = llvm::makeIntrusiveRefCnt<HLSLExternalSemaSource>();
+ if (auto SemaSource = dyn_cast_if_present<ExternalSemaSource>(
+ CI.getASTContext().getExternalSourcePtr())) {
+ auto MultiSema = llvm::makeIntrusiveRefCnt<MultiplexExternalSemaSource>(
+ std::move(SemaSource), std::move(HLSLSema));
+ CI.getASTContext().setExternalSource(std::move(MultiSema));
} else
- CI.getASTContext().setExternalSource(HLSLSema);
+ CI.getASTContext().setExternalSource(std::move(HLSLSema));
}
FailureCleanup.release();