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 | |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 137 | const std::string &OutputFile = CI.getFrontendOpts().OutputFile; |
| 138 | if (Locs.empty() && !OutputFile.empty()) { |
| 139 | // FIXME: we will issue "FIX-IT applied suggested code changes" for every |
| 140 | // input, but only the main file will actually be rewritten. |
| 141 | const std::vector<std::pair<FrontendOptions::InputKind, std::string> > &Inputs = |
| 142 | CI.getFrontendOpts().Inputs; |
| 143 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
| 144 | const FileEntry *File = CI.getFileManager().getFile(Inputs[i].second); |
| 145 | assert(File && "Input file not found in FileManager"); |
| 146 | RequestedSourceLocation Requested; |
| 147 | Requested.File = File; |
| 148 | Requested.Line = 0; |
| 149 | Requested.Column = 0; |
| 150 | FixItRewrite.addFixItLocation(Requested); |
| 151 | } |
| 152 | } |
| 153 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, |
| 158 | llvm::StringRef Filename) { |
| 159 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
| 160 | CI.getLangOpts())); |
| 161 | if (!AddFixItLocations(CI, *Rewriter)) |
| 162 | return false; |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | void FixItAction::EndSourceFileAction() { |
| 168 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
Nick Lewycky | d4a97a1 | 2010-04-15 06:46:58 +0000 | [diff] [blame] | 169 | if (!FEOpts.OutputFile.empty()) { |
| 170 | // When called with 'clang -fixit -o filename' output only the main file. |
| 171 | |
| 172 | const SourceManager &SM = getCompilerInstance().getSourceManager(); |
| 173 | FileID MainFileID = SM.getMainFileID(); |
| 174 | if (!Rewriter->IsModified(MainFileID)) { |
| 175 | getCompilerInstance().getDiagnostics().Report( |
| 176 | diag::note_fixit_main_file_unchanged); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | llvm::OwningPtr<llvm::raw_ostream> OwnedStream; |
| 181 | llvm::raw_ostream *OutFile; |
| 182 | if (FEOpts.OutputFile == "-") { |
| 183 | OutFile = &llvm::outs(); |
| 184 | } else { |
| 185 | std::string Err; |
| 186 | OutFile = new llvm::raw_fd_ostream(FEOpts.OutputFile.c_str(), Err, |
| 187 | llvm::raw_fd_ostream::F_Binary); |
| 188 | OwnedStream.reset(OutFile); |
| 189 | } |
| 190 | |
| 191 | Rewriter->WriteFixedFile(MainFileID, *OutFile); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // Otherwise rewrite all files. |
| 196 | Rewriter->WriteFixedFiles(); |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, |
| 200 | llvm::StringRef InFile) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 201 | if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) |
| 202 | return CreateObjCRewriter(InFile, OS, |
| 203 | CI.getDiagnostics(), CI.getLangOpts(), |
| 204 | CI.getDiagnosticOpts().NoRewriteMacros); |
| 205 | return 0; |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Daniel Dunbar | 8305d01 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 208 | ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, |
| 209 | llvm::StringRef InFile) { |
| 210 | return new ASTConsumer(); |
| 211 | } |
| 212 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 213 | //===----------------------------------------------------------------------===// |
| 214 | // Preprocessor Actions |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | |
| 217 | void DumpRawTokensAction::ExecuteAction() { |
| 218 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 219 | SourceManager &SM = PP.getSourceManager(); |
| 220 | |
| 221 | // Start lexing the specified input file. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 222 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
| 223 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 224 | RawLex.SetKeepWhitespaceMode(true); |
| 225 | |
| 226 | Token RawTok; |
| 227 | RawLex.LexFromRawLexer(RawTok); |
| 228 | while (RawTok.isNot(tok::eof)) { |
| 229 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 230 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 231 | RawLex.LexFromRawLexer(RawTok); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void DumpTokensAction::ExecuteAction() { |
| 236 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 237 | // Start preprocessing the specified input file. |
| 238 | Token Tok; |
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 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 33f57f8 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 244 | llvm::errs() << "\n"; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 245 | } while (Tok.isNot(tok::eof)); |
| 246 | } |
| 247 | |
| 248 | void GeneratePTHAction::ExecuteAction() { |
| 249 | CompilerInstance &CI = getCompilerInstance(); |
| 250 | if (CI.getFrontendOpts().OutputFile.empty() || |
| 251 | CI.getFrontendOpts().OutputFile == "-") { |
| 252 | // FIXME: Don't fail this way. |
| 253 | // FIXME: Verify that we can actually seek in the given file. |
Chris Lattner | 83e7a78 | 2010-04-07 22:58:06 +0000 | [diff] [blame] | 254 | llvm::report_fatal_error("PTH requires a seekable file for output!"); |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 255 | } |
| 256 | llvm::raw_fd_ostream *OS = |
| 257 | CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 258 | if (!OS) return; |
| 259 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 260 | CacheTokens(CI.getPreprocessor(), OS); |
| 261 | } |
| 262 | |
| 263 | void ParseOnlyAction::ExecuteAction() { |
| 264 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 265 | llvm::OwningPtr<Action> PA(new MinimalAction(PP)); |
| 266 | |
| 267 | Parser P(PP, *PA); |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 268 | if (PP.EnterMainSourceFile()) |
| 269 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 270 | P.ParseTranslationUnit(); |
| 271 | } |
| 272 | |
| 273 | void PreprocessOnlyAction::ExecuteAction() { |
| 274 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 275 | |
| 276 | Token Tok; |
| 277 | // Start parsing the specified input file. |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 278 | if (PP.EnterMainSourceFile()) |
| 279 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 280 | do { |
| 281 | PP.Lex(Tok); |
| 282 | } while (Tok.isNot(tok::eof)); |
| 283 | } |
| 284 | |
| 285 | void PrintParseAction::ExecuteAction() { |
| 286 | CompilerInstance &CI = getCompilerInstance(); |
| 287 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 288 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 289 | if (!OS) return; |
| 290 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 291 | llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS)); |
| 292 | |
| 293 | Parser P(PP, *PA); |
Douglas Gregor | dbf8ee6 | 2010-03-17 15:44:30 +0000 | [diff] [blame] | 294 | if (PP.EnterMainSourceFile()) |
| 295 | return; |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 296 | P.ParseTranslationUnit(); |
| 297 | } |
| 298 | |
| 299 | void PrintPreprocessedAction::ExecuteAction() { |
| 300 | CompilerInstance &CI = getCompilerInstance(); |
Steve Naroff | d57d7c0 | 2010-01-05 17:33:23 +0000 | [diff] [blame] | 301 | // Output file needs to be set to 'Binary', to avoid converting Unix style |
| 302 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
| 303 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 304 | if (!OS) return; |
| 305 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 306 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS, |
| 307 | CI.getPreprocessorOutputOpts()); |
| 308 | } |
| 309 | |
| 310 | void RewriteMacrosAction::ExecuteAction() { |
| 311 | CompilerInstance &CI = getCompilerInstance(); |
| 312 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 313 | if (!OS) return; |
| 314 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 315 | RewriteMacrosInInput(CI.getPreprocessor(), OS); |
| 316 | } |
| 317 | |
| 318 | void RewriteTestAction::ExecuteAction() { |
| 319 | CompilerInstance &CI = getCompilerInstance(); |
| 320 | llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 321 | if (!OS) return; |
| 322 | |
Daniel Dunbar | 5f3b997 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 323 | DoRewriteTest(CI.getPreprocessor(), OS); |
| 324 | } |