1 //===--- FrontendActions.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/FrontendActions.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/Lex/Preprocessor.h"
13 #include "clang/Parse/Parser.h"
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;
25 //===----------------------------------------------------------------------===//
26 // AST Consumer Actions
27 //===----------------------------------------------------------------------===//
29 ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
30 llvm::StringRef InFile) {
31 return CreateAnalysisConsumer(CI.getPreprocessor(),
32 CI.getFrontendOpts().OutputFile,
33 CI.getAnalyzerOpts());
36 ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
37 llvm::StringRef InFile) {
38 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
39 return CreateASTPrinter(OS);
43 ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
44 llvm::StringRef InFile) {
45 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
46 return CreateASTPrinterXML(OS);
50 ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
51 llvm::StringRef InFile) {
52 return CreateASTDumper();
55 ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
56 llvm::StringRef InFile) {
57 return CreateASTViewer();
60 ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
61 llvm::StringRef InFile) {
62 return CreateDeclContextPrinter();
65 ASTConsumer *DumpRecordAction::CreateASTConsumer(CompilerInstance &CI,
66 llvm::StringRef InFile) {
67 return CreateRecordLayoutDumper();
70 ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
71 llvm::StringRef InFile) {
72 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
73 if (CI.getFrontendOpts().RelocatablePCH &&
75 CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
79 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
83 if (CI.getFrontendOpts().RelocatablePCH)
84 return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
86 return CreatePCHGenerator(CI.getPreprocessor(), OS);
89 ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
90 llvm::StringRef InFile) {
91 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
92 return CreateHTMLPrinter(OS, CI.getPreprocessor());
96 ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
97 llvm::StringRef InFile) {
98 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
101 FixItAction::FixItAction() {}
102 FixItAction::~FixItAction() {}
104 ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
105 llvm::StringRef InFile) {
106 return new ASTConsumer();
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);
118 CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file)
123 RequestedSourceLocation Requested;
124 Requested.File = File;
125 Requested.Line = Locs[i].Line;
126 Requested.Column = Locs[i].Column;
127 FixItRewrite.addFixItLocation(Requested);
133 bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
134 llvm::StringRef Filename) {
135 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
137 if (!AddFixItLocations(CI, *Rewriter))
143 void FixItAction::EndSourceFileAction() {
144 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
145 Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile);
148 ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
149 llvm::StringRef InFile) {
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);
157 ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
158 llvm::StringRef InFile) {
159 return new ASTConsumer();
162 //===----------------------------------------------------------------------===//
163 // Preprocessor Actions
164 //===----------------------------------------------------------------------===//
166 void DumpRawTokensAction::ExecuteAction() {
167 Preprocessor &PP = getCompilerInstance().getPreprocessor();
168 SourceManager &SM = PP.getSourceManager();
170 // Start lexing the specified input file.
171 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
172 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
173 RawLex.SetKeepWhitespaceMode(true);
176 RawLex.LexFromRawLexer(RawTok);
177 while (RawTok.isNot(tok::eof)) {
178 PP.DumpToken(RawTok, true);
179 llvm::errs() << "\n";
180 RawLex.LexFromRawLexer(RawTok);
184 void DumpTokensAction::ExecuteAction() {
185 Preprocessor &PP = getCompilerInstance().getPreprocessor();
186 // Start preprocessing the specified input file.
188 PP.EnterMainSourceFile();
191 PP.DumpToken(Tok, true);
192 llvm::errs() << "\n";
193 } while (Tok.isNot(tok::eof));
196 void GeneratePTHAction::ExecuteAction() {
197 CompilerInstance &CI = getCompilerInstance();
198 if (CI.getFrontendOpts().OutputFile.empty() ||
199 CI.getFrontendOpts().OutputFile == "-") {
200 // FIXME: Don't fail this way.
201 // FIXME: Verify that we can actually seek in the given file.
202 llvm::llvm_report_error("PTH requires a seekable file for output!");
204 llvm::raw_fd_ostream *OS =
205 CI.createDefaultOutputFile(true, getCurrentFile());
208 CacheTokens(CI.getPreprocessor(), OS);
211 void ParseOnlyAction::ExecuteAction() {
212 Preprocessor &PP = getCompilerInstance().getPreprocessor();
213 llvm::OwningPtr<Action> PA(new MinimalAction(PP));
216 PP.EnterMainSourceFile();
217 P.ParseTranslationUnit();
220 void PreprocessOnlyAction::ExecuteAction() {
221 Preprocessor &PP = getCompilerInstance().getPreprocessor();
224 // Start parsing the specified input file.
225 PP.EnterMainSourceFile();
228 } while (Tok.isNot(tok::eof));
231 void PrintParseAction::ExecuteAction() {
232 CompilerInstance &CI = getCompilerInstance();
233 Preprocessor &PP = getCompilerInstance().getPreprocessor();
234 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
237 llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
240 PP.EnterMainSourceFile();
241 P.ParseTranslationUnit();
244 void PrintPreprocessedAction::ExecuteAction() {
245 CompilerInstance &CI = getCompilerInstance();
246 // Output file needs to be set to 'Binary', to avoid converting Unix style
247 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
248 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
251 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
252 CI.getPreprocessorOutputOpts());
255 void RewriteMacrosAction::ExecuteAction() {
256 CompilerInstance &CI = getCompilerInstance();
257 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
260 RewriteMacrosInInput(CI.getPreprocessor(), OS);
263 void RewriteTestAction::ExecuteAction() {
264 CompilerInstance &CI = getCompilerInstance();
265 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
268 DoRewriteTest(CI.getPreprocessor(), OS);