Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 1 | //===--- FrontendActions.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 | #include "clang/Frontend/FrontendActions.h" |
| 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 12 | #include "clang/Lex/Preprocessor.h" |
| 13 | #include "clang/Parse/Parser.h" |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Frontend/AnalysisConsumer.h" |
| 16 | #include "clang/Frontend/ASTConsumers.h" |
| 17 | #include "clang/Frontend/ASTUnit.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Frontend/FixItRewriter.h" |
| 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Frontend/Utils.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | using namespace clang; |
| 24 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 2758595 | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 26 | // Custom Actions |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
| 30 | llvm::StringRef InFile) { |
| 31 | return new ASTConsumer(); |
| 32 | } |
| 33 | |
| 34 | void InitOnlyAction::ExecuteAction() { |
| 35 | } |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 38 | // AST Consumer Actions |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 41 | ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI, |
| 42 | llvm::StringRef InFile) { |
| 43 | return CreateAnalysisConsumer(CI.getPreprocessor(), |
| 44 | CI.getFrontendOpts().OutputFile, |
| 45 | CI.getAnalyzerOpts()); |
| 46 | } |
| 47 | |
| 48 | ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 49 | llvm::StringRef InFile) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 50 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
| 51 | return CreateASTPrinter(OS); |
| 52 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI, |
| 56 | llvm::StringRef InFile) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 57 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml")) |
| 58 | return CreateASTPrinterXML(OS); |
| 59 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, |
| 63 | llvm::StringRef InFile) { |
| 64 | return CreateASTDumper(); |
| 65 | } |
| 66 | |
| 67 | ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI, |
| 68 | llvm::StringRef InFile) { |
| 69 | return CreateASTViewer(); |
| 70 | } |
| 71 | |
| 72 | ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 73 | llvm::StringRef InFile) { |
| 74 | return CreateDeclContextPrinter(); |
| 75 | } |
| 76 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 77 | ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, |
| 78 | llvm::StringRef InFile) { |
| 79 | const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; |
| 80 | if (CI.getFrontendOpts().RelocatablePCH && |
| 81 | Sysroot.empty()) { |
| 82 | CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 87 | if (!OS) |
| 88 | return 0; |
| 89 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 90 | if (CI.getFrontendOpts().RelocatablePCH) |
| 91 | return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str()); |
| 92 | |
| 93 | return CreatePCHGenerator(CI.getPreprocessor(), OS); |
| 94 | } |
| 95 | |
| 96 | ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 97 | llvm::StringRef InFile) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 98 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) |
| 99 | return CreateHTMLPrinter(OS, CI.getPreprocessor()); |
| 100 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI, |
| 104 | llvm::StringRef InFile) { |
| 105 | return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance); |
| 106 | } |
| 107 | |
| 108 | FixItAction::FixItAction() {} |
| 109 | FixItAction::~FixItAction() {} |
| 110 | |
| 111 | ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI, |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 112 | llvm::StringRef InFile) { |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 113 | return new ASTConsumer(); |
| 114 | } |
| 115 | |
| 116 | /// AddFixItLocations - Add any individual user specified "fix-it" locations, |
| 117 | /// and return true on success. |
| 118 | static bool AddFixItLocations(CompilerInstance &CI, |
| 119 | FixItRewriter &FixItRewrite) { |
| 120 | const std::vector<ParsedSourceLocation> &Locs = |
| 121 | CI.getFrontendOpts().FixItLocations; |
| 122 | for (unsigned i = 0, e = Locs.size(); i != e; ++i) { |
| 123 | const FileEntry *File = CI.getFileManager().getFile(Locs[i].FileName); |
| 124 | if (!File) { |
| 125 | CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file) |
| 126 | << Locs[i].FileName; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | RequestedSourceLocation Requested; |
| 131 | Requested.File = File; |
| 132 | Requested.Line = Locs[i].Line; |
| 133 | Requested.Column = Locs[i].Column; |
| 134 | FixItRewrite.addFixItLocation(Requested); |
| 135 | } |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, |
| 141 | llvm::StringRef Filename) { |
| 142 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 143 | CI.getLangOpts())); |
| 144 | if (!AddFixItLocations(CI, *Rewriter)) |
| 145 | return false; |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | void FixItAction::EndSourceFileAction() { |
| 151 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
| 152 | Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile); |
| 153 | } |
| 154 | |
| 155 | ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, |
| 156 | llvm::StringRef InFile) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 157 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) |
| 158 | return CreateObjCRewriter(InFile, OS, |
| 159 | CI.getDiagnostics(), CI.getLangOpts(), |
| 160 | CI.getDiagnosticOpts().NoRewriteMacros); |
| 161 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 164 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
| 165 | llvm::StringRef InFile) { |
| 166 | return new ASTConsumer(); |
| 167 | } |
| 168 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 169 | //===----------------------------------------------------------------------===// |
| 170 | // Preprocessor Actions |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | |
| 173 | void DumpRawTokensAction::ExecuteAction() { |
| 174 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 175 | SourceManager &SM = PP.getSourceManager(); |
| 176 | |
| 177 | // Start lexing the specified input file. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 178 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
| 179 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 180 | RawLex.SetKeepWhitespaceMode(true); |
| 181 | |
| 182 | Token RawTok; |
| 183 | RawLex.LexFromRawLexer(RawTok); |
| 184 | while (RawTok.isNot(tok::eof)) { |
| 185 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 186 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 187 | RawLex.LexFromRawLexer(RawTok); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void DumpTokensAction::ExecuteAction() { |
| 192 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 193 | // Start preprocessing the specified input file. |
| 194 | Token Tok; |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 195 | if (PP.EnterMainSourceFile()) |
| 196 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 197 | do { |
| 198 | PP.Lex(Tok); |
| 199 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 200 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 201 | } while (Tok.isNot(tok::eof)); |
| 202 | } |
| 203 | |
| 204 | void GeneratePTHAction::ExecuteAction() { |
| 205 | CompilerInstance &CI = getCompilerInstance(); |
| 206 | if (CI.getFrontendOpts().OutputFile.empty() || |
| 207 | CI.getFrontendOpts().OutputFile == "-") { |
| 208 | // FIXME: Don't fail this way. |
| 209 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 83e7a78 | 2010-04-07 22:58:06 +0000 | [diff] [blame] | 210 | llvm::report_fatal_error("PTH requires a seekable file for output!"); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 211 | } |
| 212 | llvm::raw_fd_ostream *OS = |
| 213 | CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 214 | if (!OS) return; |
| 215 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 216 | CacheTokens(CI.getPreprocessor(), OS); |
| 217 | } |
| 218 | |
| 219 | void ParseOnlyAction::ExecuteAction() { |
| 220 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 221 | llvm::OwningPtr<Action> PA(new MinimalAction(PP)); |
| 222 | |
| 223 | Parser P(PP, *PA); |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 224 | if (PP.EnterMainSourceFile()) |
| 225 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 226 | P.ParseTranslationUnit(); |
| 227 | } |
| 228 | |
| 229 | void PreprocessOnlyAction::ExecuteAction() { |
| 230 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 231 | |
| 232 | Token Tok; |
| 233 | // Start parsing the specified input file. |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 234 | if (PP.EnterMainSourceFile()) |
| 235 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 236 | do { |
| 237 | PP.Lex(Tok); |
| 238 | } while (Tok.isNot(tok::eof)); |
| 239 | } |
| 240 | |
| 241 | void PrintParseAction::ExecuteAction() { |
| 242 | CompilerInstance &CI = getCompilerInstance(); |
| 243 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 244 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 245 | if (!OS) return; |
| 246 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 247 | llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS)); |
| 248 | |
| 249 | Parser P(PP, *PA); |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 250 | if (PP.EnterMainSourceFile()) |
| 251 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 252 | P.ParseTranslationUnit(); |
| 253 | } |
| 254 | |
| 255 | void PrintPreprocessedAction::ExecuteAction() { |
| 256 | CompilerInstance &CI = getCompilerInstance(); |
Steve Naroff | d57d7c0 | 2010-01-05 17:33:23 +0000 | [diff] [blame] | 257 | // Output file needs to be set to 'Binary', to avoid converting Unix style |
| 258 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
| 259 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 260 | if (!OS) return; |
| 261 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 262 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS, |
| 263 | CI.getPreprocessorOutputOpts()); |
| 264 | } |
| 265 | |
| 266 | void RewriteMacrosAction::ExecuteAction() { |
| 267 | CompilerInstance &CI = getCompilerInstance(); |
| 268 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 269 | if (!OS) return; |
| 270 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 271 | RewriteMacrosInInput(CI.getPreprocessor(), OS); |
| 272 | } |
| 273 | |
| 274 | void RewriteTestAction::ExecuteAction() { |
| 275 | CompilerInstance &CI = getCompilerInstance(); |
| 276 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 277 | if (!OS) return; |
| 278 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 279 | DoRewriteTest(CI.getPreprocessor(), OS); |
| 280 | } |