FrontendActionTest.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction tests ---===//
  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. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/AST/RecursiveASTVisitor.h"
  12. #include "clang/Frontend/CompilerInstance.h"
  13. #include "clang/Frontend/CompilerInvocation.h"
  14. #include "clang/Frontend/FrontendAction.h"
  15. #include "clang/Frontend/FrontendActions.h"
  16. #include "clang/Lex/Preprocessor.h"
  17. #include "clang/Lex/PreprocessorOptions.h"
  18. #include "clang/Sema/Sema.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "gtest/gtest.h"
  22. using namespace llvm;
  23. using namespace clang;
  24. namespace {
  25. class TestASTFrontendAction : public ASTFrontendAction {
  26. public:
  27. TestASTFrontendAction(bool enableIncrementalProcessing = false,
  28. bool actOnEndOfTranslationUnit = false)
  29. : EnableIncrementalProcessing(enableIncrementalProcessing),
  30. ActOnEndOfTranslationUnit(actOnEndOfTranslationUnit) { }
  31. bool EnableIncrementalProcessing;
  32. bool ActOnEndOfTranslationUnit;
  33. std::vector<std::string> decl_names;
  34. bool BeginSourceFileAction(CompilerInstance &ci) override {
  35. if (EnableIncrementalProcessing)
  36. ci.getPreprocessor().enableIncrementalProcessing();
  37. return ASTFrontendAction::BeginSourceFileAction(ci);
  38. }
  39. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  40. StringRef InFile) override {
  41. return llvm::make_unique<Visitor>(CI, ActOnEndOfTranslationUnit,
  42. decl_names);
  43. }
  44. private:
  45. class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
  46. public:
  47. Visitor(CompilerInstance &CI, bool ActOnEndOfTranslationUnit,
  48. std::vector<std::string> &decl_names) :
  49. CI(CI), ActOnEndOfTranslationUnit(ActOnEndOfTranslationUnit),
  50. decl_names_(decl_names) {}
  51. void HandleTranslationUnit(ASTContext &context) override {
  52. if (ActOnEndOfTranslationUnit) {
  53. CI.getSema().ActOnEndOfTranslationUnit();
  54. }
  55. TraverseDecl(context.getTranslationUnitDecl());
  56. }
  57. virtual bool VisitNamedDecl(NamedDecl *Decl) {
  58. decl_names_.push_back(Decl->getQualifiedNameAsString());
  59. return true;
  60. }
  61. private:
  62. CompilerInstance &CI;
  63. bool ActOnEndOfTranslationUnit;
  64. std::vector<std::string> &decl_names_;
  65. };
  66. };
  67. TEST(ASTFrontendAction, Sanity) {
  68. auto invocation = std::make_shared<CompilerInvocation>();
  69. invocation->getPreprocessorOpts().addRemappedFile(
  70. "test.cc",
  71. MemoryBuffer::getMemBuffer("int main() { float x; }").release());
  72. invocation->getFrontendOpts().Inputs.push_back(
  73. FrontendInputFile("test.cc", InputKind::CXX));
  74. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  75. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  76. CompilerInstance compiler;
  77. compiler.setInvocation(std::move(invocation));
  78. compiler.createDiagnostics();
  79. TestASTFrontendAction test_action;
  80. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  81. ASSERT_EQ(2U, test_action.decl_names.size());
  82. EXPECT_EQ("main", test_action.decl_names[0]);
  83. EXPECT_EQ("x", test_action.decl_names[1]);
  84. }
  85. TEST(ASTFrontendAction, IncrementalParsing) {
  86. auto invocation = std::make_shared<CompilerInvocation>();
  87. invocation->getPreprocessorOpts().addRemappedFile(
  88. "test.cc",
  89. MemoryBuffer::getMemBuffer("int main() { float x; }").release());
  90. invocation->getFrontendOpts().Inputs.push_back(
  91. FrontendInputFile("test.cc", InputKind::CXX));
  92. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  93. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  94. CompilerInstance compiler;
  95. compiler.setInvocation(std::move(invocation));
  96. compiler.createDiagnostics();
  97. TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
  98. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  99. ASSERT_EQ(2U, test_action.decl_names.size());
  100. EXPECT_EQ("main", test_action.decl_names[0]);
  101. EXPECT_EQ("x", test_action.decl_names[1]);
  102. }
  103. TEST(ASTFrontendAction, LateTemplateIncrementalParsing) {
  104. auto invocation = std::make_shared<CompilerInvocation>();
  105. invocation->getLangOpts()->CPlusPlus = true;
  106. invocation->getLangOpts()->DelayedTemplateParsing = true;
  107. invocation->getPreprocessorOpts().addRemappedFile(
  108. "test.cc", MemoryBuffer::getMemBuffer(
  109. "template<typename T> struct A { A(T); T data; };\n"
  110. "template<typename T> struct B: public A<T> {\n"
  111. " B();\n"
  112. " B(B const& b): A<T>(b.data) {}\n"
  113. "};\n"
  114. "B<char> c() { return B<char>(); }\n").release());
  115. invocation->getFrontendOpts().Inputs.push_back(
  116. FrontendInputFile("test.cc", InputKind::CXX));
  117. invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  118. invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  119. CompilerInstance compiler;
  120. compiler.setInvocation(std::move(invocation));
  121. compiler.createDiagnostics();
  122. TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true,
  123. /*actOnEndOfTranslationUnit=*/true);
  124. ASSERT_TRUE(compiler.ExecuteAction(test_action));
  125. ASSERT_EQ(13U, test_action.decl_names.size());
  126. EXPECT_EQ("A", test_action.decl_names[0]);
  127. EXPECT_EQ("c", test_action.decl_names[12]);
  128. }
  129. struct TestPPCallbacks : public PPCallbacks {
  130. TestPPCallbacks() : SeenEnd(false) {}
  131. void EndOfMainFile() override { SeenEnd = true; }
  132. bool SeenEnd;
  133. };
  134. class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
  135. TestPPCallbacks *Callbacks;
  136. public:
  137. TestPPCallbacksFrontendAction(TestPPCallbacks *C)
  138. : Callbacks(C), SeenEnd(false) {}
  139. void ExecuteAction() override {
  140. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  141. PP.addPPCallbacks(std::unique_ptr<TestPPCallbacks>(Callbacks));
  142. PP.EnterMainSourceFile();
  143. }
  144. void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
  145. bool SeenEnd;
  146. };
  147. TEST(PreprocessorFrontendAction, EndSourceFile) {
  148. auto Invocation = std::make_shared<CompilerInvocation>();
  149. Invocation->getPreprocessorOpts().addRemappedFile(
  150. "test.cc",
  151. MemoryBuffer::getMemBuffer("int main() { float x; }").release());
  152. Invocation->getFrontendOpts().Inputs.push_back(
  153. FrontendInputFile("test.cc", InputKind::CXX));
  154. Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  155. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  156. CompilerInstance Compiler;
  157. Compiler.setInvocation(std::move(Invocation));
  158. Compiler.createDiagnostics();
  159. TestPPCallbacks *Callbacks = new TestPPCallbacks;
  160. TestPPCallbacksFrontendAction TestAction(Callbacks);
  161. ASSERT_FALSE(Callbacks->SeenEnd);
  162. ASSERT_FALSE(TestAction.SeenEnd);
  163. ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
  164. // Check that EndOfMainFile was called before EndSourceFileAction.
  165. ASSERT_TRUE(TestAction.SeenEnd);
  166. }
  167. class TypoExternalSemaSource : public ExternalSemaSource {
  168. CompilerInstance &CI;
  169. public:
  170. TypoExternalSemaSource(CompilerInstance &CI) : CI(CI) {}
  171. TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
  172. Scope *S, CXXScopeSpec *SS,
  173. CorrectionCandidateCallback &CCC,
  174. DeclContext *MemberContext, bool EnteringContext,
  175. const ObjCObjectPointerType *OPT) override {
  176. // Generate a fake typo correction with one attached note.
  177. ASTContext &Ctx = CI.getASTContext();
  178. TypoCorrection TC(DeclarationName(&Ctx.Idents.get("moo")));
  179. unsigned DiagID = Ctx.getDiagnostics().getCustomDiagID(
  180. DiagnosticsEngine::Note, "This is a note");
  181. TC.addExtraDiagnostic(PartialDiagnostic(DiagID, Ctx.getDiagAllocator()));
  182. return TC;
  183. }
  184. };
  185. struct TypoDiagnosticConsumer : public DiagnosticConsumer {
  186. void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
  187. const Diagnostic &Info) override {
  188. // Capture errors and notes. There should be one of each.
  189. if (DiagLevel == DiagnosticsEngine::Error) {
  190. assert(Error.empty());
  191. Info.FormatDiagnostic(Error);
  192. } else {
  193. assert(Note.empty());
  194. Info.FormatDiagnostic(Note);
  195. }
  196. }
  197. SmallString<32> Error;
  198. SmallString<32> Note;
  199. };
  200. TEST(ASTFrontendAction, ExternalSemaSource) {
  201. auto Invocation = std::make_shared<CompilerInvocation>();
  202. Invocation->getLangOpts()->CPlusPlus = true;
  203. Invocation->getPreprocessorOpts().addRemappedFile(
  204. "test.cc", MemoryBuffer::getMemBuffer("void fooo();\n"
  205. "int main() { foo(); }")
  206. .release());
  207. Invocation->getFrontendOpts().Inputs.push_back(
  208. FrontendInputFile("test.cc", InputKind::CXX));
  209. Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
  210. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  211. CompilerInstance Compiler;
  212. Compiler.setInvocation(std::move(Invocation));
  213. auto *TDC = new TypoDiagnosticConsumer;
  214. Compiler.createDiagnostics(TDC, /*ShouldOwnClient=*/true);
  215. Compiler.setExternalSemaSource(new TypoExternalSemaSource(Compiler));
  216. SyntaxOnlyAction TestAction;
  217. ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
  218. // There should be one error correcting to 'moo' and a note attached to it.
  219. EXPECT_EQ("use of undeclared identifier 'foo'; did you mean 'moo'?",
  220. TDC->Error.str().str());
  221. EXPECT_EQ("This is a note", TDC->Note.str().str());
  222. }
  223. } // anonymous namespace