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