FrontendActionTest.cpp 9.7 KB

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