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