blob: 694cadcd13c063d01483d075e8f4926b5613bd90 [file] [log] [blame]
Daniel Dunbar8305d012009-11-14 10:42:46 +00001//===--- 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 Dunbarc72cc502010-06-11 20:10:12 +000012#include "clang/Lex/Pragma.h"
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000013#include "clang/Lex/Preprocessor.h"
14#include "clang/Parse/Parser.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000016#include "clang/Frontend/ASTConsumers.h"
17#include "clang/Frontend/ASTUnit.h"
18#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000019#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Frontend/Utils.h"
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000021#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000025//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000026// Custom Actions
27//===----------------------------------------------------------------------===//
28
29ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
30 llvm::StringRef InFile) {
31 return new ASTConsumer();
32}
33
34void InitOnlyAction::ExecuteAction() {
35}
36
37//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000038// AST Consumer Actions
39//===----------------------------------------------------------------------===//
40
Daniel Dunbar8305d012009-11-14 10:42:46 +000041ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
42 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000043 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
44 return CreateASTPrinter(OS);
45 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000046}
47
48ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
49 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000050 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
51 return CreateASTPrinterXML(OS);
52 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000053}
54
55ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
56 llvm::StringRef InFile) {
57 return CreateASTDumper();
58}
59
60ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
61 llvm::StringRef InFile) {
62 return CreateASTViewer();
63}
64
65ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
66 llvm::StringRef InFile) {
67 return CreateDeclContextPrinter();
68}
69
Daniel Dunbar8305d012009-11-14 10:42:46 +000070ASTConsumer *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 Dunbar36043592009-12-03 09:13:30 +000080 if (!OS)
81 return 0;
82
Daniel Dunbar8305d012009-11-14 10:42:46 +000083 if (CI.getFrontendOpts().RelocatablePCH)
Sebastian Redla93e3b52010-07-08 22:01:51 +000084 return CreatePCHGenerator(CI.getPreprocessor(), OS,
Sebastian Redld6ac4522010-07-09 00:00:58 +000085 CI.getInvocation().getFrontendOpts().ChainedPCH ?
86 CI.getPCHReader() : 0,
87 Sysroot.c_str());
Daniel Dunbar8305d012009-11-14 10:42:46 +000088
Sebastian Redla93e3b52010-07-08 22:01:51 +000089 return CreatePCHGenerator(CI.getPreprocessor(), OS, CI.getPCHReader());
Daniel Dunbar8305d012009-11-14 10:42:46 +000090}
91
Daniel Dunbar8305d012009-11-14 10:42:46 +000092ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
93 llvm::StringRef InFile) {
94 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
95}
96
Daniel Dunbar8305d012009-11-14 10:42:46 +000097ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
98 llvm::StringRef InFile) {
99 return new ASTConsumer();
100}
101
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000102//===----------------------------------------------------------------------===//
103// Preprocessor Actions
104//===----------------------------------------------------------------------===//
105
106void DumpRawTokensAction::ExecuteAction() {
107 Preprocessor &PP = getCompilerInstance().getPreprocessor();
108 SourceManager &SM = PP.getSourceManager();
109
110 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000111 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
112 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000113 RawLex.SetKeepWhitespaceMode(true);
114
115 Token RawTok;
116 RawLex.LexFromRawLexer(RawTok);
117 while (RawTok.isNot(tok::eof)) {
118 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000119 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000120 RawLex.LexFromRawLexer(RawTok);
121 }
122}
123
124void DumpTokensAction::ExecuteAction() {
125 Preprocessor &PP = getCompilerInstance().getPreprocessor();
126 // Start preprocessing the specified input file.
127 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000128 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000129 do {
130 PP.Lex(Tok);
131 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000132 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000133 } while (Tok.isNot(tok::eof));
134}
135
136void GeneratePTHAction::ExecuteAction() {
137 CompilerInstance &CI = getCompilerInstance();
138 if (CI.getFrontendOpts().OutputFile.empty() ||
139 CI.getFrontendOpts().OutputFile == "-") {
140 // FIXME: Don't fail this way.
141 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000142 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000143 }
144 llvm::raw_fd_ostream *OS =
145 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000146 if (!OS) return;
147
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000148 CacheTokens(CI.getPreprocessor(), OS);
149}
150
151void ParseOnlyAction::ExecuteAction() {
152 Preprocessor &PP = getCompilerInstance().getPreprocessor();
153 llvm::OwningPtr<Action> PA(new MinimalAction(PP));
154
155 Parser P(PP, *PA);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000156 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000157 P.ParseTranslationUnit();
158}
159
160void PreprocessOnlyAction::ExecuteAction() {
161 Preprocessor &PP = getCompilerInstance().getPreprocessor();
162
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000163 // Ignore unknown pragmas.
164 PP.AddPragmaHandler(0, new EmptyPragmaHandler());
165
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000166 Token Tok;
167 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000168 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000169 do {
170 PP.Lex(Tok);
171 } while (Tok.isNot(tok::eof));
172}
173
174void PrintParseAction::ExecuteAction() {
175 CompilerInstance &CI = getCompilerInstance();
176 Preprocessor &PP = getCompilerInstance().getPreprocessor();
177 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000178 if (!OS) return;
179
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000180 llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
181
182 Parser P(PP, *PA);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000183 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000184 P.ParseTranslationUnit();
185}
186
187void PrintPreprocessedAction::ExecuteAction() {
188 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000189 // Output file needs to be set to 'Binary', to avoid converting Unix style
190 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
191 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000192 if (!OS) return;
193
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000194 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
195 CI.getPreprocessorOutputOpts());
196}