diff options
author | Sean Callanan <scallanan@apple.com> | 2017-04-11 19:33:35 +0000 |
---|---|---|
committer | Sean Callanan <scallanan@apple.com> | 2017-04-11 19:33:35 +0000 |
commit | 3975e67148c7382405ddd5b9004cdad6836cf443 (patch) | |
tree | 3445341e265534e27aa5561d2788b76f6ebed5aa /tools/clang-import-test | |
parent | 44e7dd4bbbba32a53c0c57a3de45904b5fb084d6 (diff) |
[clang-import-test] Lookup inside contexts
clang-import-test has until now been only able to report top-level Decls.
This is clearly insufficient; we should be able to look inside structs
and namespaces also. This patch adds new test cases for a variety of
lookups inside existing ASTContexts, and adds the functionality necessar
to make most of these testcases work. (One testcase is known to fail
because of ASTImporter limitations when importing templates; I'll look
into that separately.)
This patch also separates the core functionality out into
ExternalASTMerger, an interface that allows clients like LLDB to make
use of it. clang-import-test now only has the machinery necessary to
set up the tests.
Differential revision: https://reviews.llvm.org/D30435
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299976 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-import-test')
-rw-r--r-- | tools/clang-import-test/clang-import-test.cpp | 64 |
1 files changed, 12 insertions, 52 deletions
diff --git a/tools/clang-import-test/clang-import-test.cpp b/tools/clang-import-test/clang-import-test.cpp index 33190af4bf..d7ab18478c 100644 --- a/tools/clang-import-test/clang-import-test.cpp +++ b/tools/clang-import-test/clang-import-test.cpp @@ -9,6 +9,8 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/ExternalASTMerger.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/SourceLocation.h" @@ -189,61 +191,18 @@ std::unique_ptr<CodeGenerator> BuildCodeGen(CompilerInstance &CI, } // end namespace namespace { -class TestExternalASTSource : public ExternalASTSource { -private: - llvm::ArrayRef<std::unique_ptr<CompilerInstance>> ImportCIs; - std::map<CompilerInstance *, std::unique_ptr<ASTImporter>> ForwardImporters; - std::map<CompilerInstance *, std::unique_ptr<ASTImporter>> ReverseImporters; - -public: - TestExternalASTSource( - CompilerInstance &ExpressionCI, - llvm::ArrayRef<std::unique_ptr<CompilerInstance>> ImportCIs) - : ImportCIs(ImportCIs) { - for (const std::unique_ptr<CompilerInstance> &ImportCI : ImportCIs) { - ForwardImporters[ImportCI.get()] = llvm::make_unique<ASTImporter>( - ExpressionCI.getASTContext(), ExpressionCI.getFileManager(), - ImportCI->getASTContext(), ImportCI->getFileManager(), - /*MinimalImport=*/true); - ReverseImporters[ImportCI.get()] = llvm::make_unique<ASTImporter>( - ImportCI->getASTContext(), ImportCI->getFileManager(), - ExpressionCI.getASTContext(), ExpressionCI.getFileManager(), - /*MinimalImport=*/true); - } - } - - bool FindExternalVisibleDeclsByName(const DeclContext *DC, - DeclarationName Name) override { - llvm::SmallVector<NamedDecl *, 1> Decls; - - if (isa<TranslationUnitDecl>(DC)) { - for (const std::unique_ptr<CompilerInstance> &I : ImportCIs) { - DeclarationName FromName = ReverseImporters[I.get()]->Import(Name); - DeclContextLookupResult Result = - I->getASTContext().getTranslationUnitDecl()->lookup(FromName); - for (NamedDecl *FromD : Result) { - NamedDecl *D = - llvm::cast<NamedDecl>(ForwardImporters[I.get()]->Import(FromD)); - Decls.push_back(D); - } - } - } - if (Decls.empty()) { - return false; - } else { - SetExternalVisibleDeclsForName(DC, Name, Decls); - return true; - } - } -}; - + void AddExternalSource( CompilerInstance &CI, llvm::ArrayRef<std::unique_ptr<CompilerInstance>> Imports) { - ASTContext &AST = CI.getASTContext(); - auto ES = llvm::make_unique<TestExternalASTSource>(CI, Imports); - AST.setExternalSource(ES.release()); - AST.getTranslationUnitDecl()->setHasExternalVisibleStorage(); + ExternalASTMerger::ImporterEndpoint Target({CI.getASTContext(), CI.getFileManager()}); + llvm::SmallVector<ExternalASTMerger::ImporterEndpoint, 3> Sources; + for (const std::unique_ptr<CompilerInstance> &CI : Imports) { + Sources.push_back({CI->getASTContext(), CI->getFileManager()}); + } + auto ES = llvm::make_unique<ExternalASTMerger>(Target, Sources); + CI.getASTContext().setExternalSource(ES.release()); + CI.getASTContext().getTranslationUnitDecl()->setHasExternalVisibleStorage(); } llvm::Error ParseSource(const std::string &Path, CompilerInstance &CI, @@ -292,6 +251,7 @@ Parse(const std::string &Path, return std::move(CI); } } + } // end namespace int main(int argc, const char **argv) { |