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