ExternalASTSourceTest.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===- unittest/AST/ExternalASTSourceTest.cpp -----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains tests for Clang's ExternalASTSource.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTConsumer.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ExternalASTSource.h"
  16. #include "clang/Frontend/CompilerInstance.h"
  17. #include "clang/Frontend/CompilerInvocation.h"
  18. #include "clang/Frontend/FrontendActions.h"
  19. #include "gtest/gtest.h"
  20. using namespace clang;
  21. using namespace llvm;
  22. class TestFrontendAction : public ASTFrontendAction {
  23. public:
  24. TestFrontendAction(ExternalASTSource *Source) : Source(Source) {}
  25. private:
  26. void ExecuteAction() override {
  27. getCompilerInstance().getASTContext().setExternalSource(Source);
  28. getCompilerInstance().getASTContext().getTranslationUnitDecl()
  29. ->setHasExternalVisibleStorage();
  30. return ASTFrontendAction::ExecuteAction();
  31. }
  32. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  33. StringRef InFile) override {
  34. return llvm::make_unique<ASTConsumer>();
  35. }
  36. IntrusiveRefCntPtr<ExternalASTSource> Source;
  37. };
  38. bool testExternalASTSource(ExternalASTSource *Source,
  39. StringRef FileContents) {
  40. CompilerInstance Compiler;
  41. Compiler.createDiagnostics();
  42. CompilerInvocation *Invocation = new CompilerInvocation;
  43. Invocation->getPreprocessorOpts().addRemappedFile(
  44. "test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
  45. const char *Args[] = { "test.cc" };
  46. CompilerInvocation::CreateFromArgs(*Invocation, Args,
  47. Args + array_lengthof(Args),
  48. Compiler.getDiagnostics());
  49. Compiler.setInvocation(Invocation);
  50. TestFrontendAction Action(Source);
  51. return Compiler.ExecuteAction(Action);
  52. }
  53. // Ensure that a failed name lookup into an external source only occurs once.
  54. TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
  55. struct TestSource : ExternalASTSource {
  56. TestSource(unsigned &Calls) : Calls(Calls) {}
  57. bool FindExternalVisibleDeclsByName(const DeclContext *,
  58. DeclarationName Name) override {
  59. if (Name.getAsString() == "j")
  60. ++Calls;
  61. return false;
  62. }
  63. unsigned &Calls;
  64. };
  65. unsigned Calls = 0;
  66. ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;"));
  67. EXPECT_EQ(1u, Calls);
  68. }