ExternalASTSourceTest.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //===- unittest/AST/ExternalASTSourceTest.cpp -----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains tests for Clang's ExternalASTSource.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/ASTConsumer.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/ExternalASTSource.h"
  15. #include "clang/Frontend/CompilerInstance.h"
  16. #include "clang/Frontend/CompilerInvocation.h"
  17. #include "clang/Frontend/FrontendActions.h"
  18. #include "clang/Lex/PreprocessorOptions.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 std::make_unique<ASTConsumer>();
  35. }
  36. IntrusiveRefCntPtr<ExternalASTSource> Source;
  37. };
  38. bool testExternalASTSource(ExternalASTSource *Source,
  39. StringRef FileContents) {
  40. CompilerInstance Compiler;
  41. Compiler.createDiagnostics();
  42. auto Invocation = std::make_shared<CompilerInvocation>();
  43. Invocation->getPreprocessorOpts().addRemappedFile(
  44. "test.cc", MemoryBuffer::getMemBuffer(FileContents).release());
  45. const char *Args[] = { "test.cc" };
  46. CompilerInvocation::CreateFromArgs(*Invocation, Args,
  47. Compiler.getDiagnostics());
  48. Compiler.setInvocation(std::move(Invocation));
  49. TestFrontendAction Action(Source);
  50. return Compiler.ExecuteAction(Action);
  51. }
  52. // Ensure that a failed name lookup into an external source only occurs once.
  53. TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
  54. struct TestSource : ExternalASTSource {
  55. TestSource(unsigned &Calls) : Calls(Calls) {}
  56. bool FindExternalVisibleDeclsByName(const DeclContext *,
  57. DeclarationName Name) override {
  58. if (Name.getAsString() == "j")
  59. ++Calls;
  60. return false;
  61. }
  62. unsigned &Calls;
  63. };
  64. unsigned Calls = 0;
  65. ASSERT_TRUE(testExternalASTSource(new TestSource(Calls), "int j, k = j;"));
  66. EXPECT_EQ(1u, Calls);
  67. }