 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
54.7% |
35 / 64 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
84.4% |
54 / 64 |
| |
|
Line Coverage: |
73.1% |
114 / 156 |
| |
 |
|
 |
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"
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;
24 :
25 : //===----------------------------------------------------------------------===//
26 : // AST Consumer Actions
27 : //===----------------------------------------------------------------------===//
28 :
29 : ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
30 188: llvm::StringRef InFile) {
31 : return CreateAnalysisConsumer(CI.getPreprocessor(),
32 : CI.getFrontendOpts().OutputFile,
33 188: CI.getAnalyzerOpts());
34 : }
35 :
36 : ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
37 17: llvm::StringRef InFile) {
17: branch 2 taken
0: branch 3 not taken
38 17: if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
39 17: return CreateASTPrinter(OS);
40 0: return 0;
41 : }
42 :
43 : ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
44 1: llvm::StringRef InFile) {
1: branch 2 taken
0: branch 3 not taken
45 1: if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
46 1: return CreateASTPrinterXML(OS);
47 0: return 0;
48 : }
49 :
50 : ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
51 5: llvm::StringRef InFile) {
52 5: return CreateASTDumper();
53 : }
54 :
55 : ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
56 0: llvm::StringRef InFile) {
57 0: return CreateASTViewer();
58 : }
59 :
60 : ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
61 2: llvm::StringRef InFile) {
62 2: return CreateDeclContextPrinter();
63 : }
64 :
65 : ASTConsumer *DumpRecordAction::CreateASTConsumer(CompilerInstance &CI,
66 1: llvm::StringRef InFile) {
67 1: return CreateRecordLayoutDumper();
68 : }
69 :
70 : ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
71 44: llvm::StringRef InFile) {
72 44: const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
1: branch 1 taken
43: branch 2 taken
0: branch 4 not taken
1: branch 5 taken
0: branch 6 not taken
44: branch 7 taken
73 44: if (CI.getFrontendOpts().RelocatablePCH &&
74 : Sysroot.empty()) {
75 0: CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
76 0: return 0;
77 : }
78 :
79 44: llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
0: branch 0 not taken
44: branch 1 taken
80 44: if (!OS)
81 0: return 0;
82 :
1: branch 1 taken
43: branch 2 taken
83 44: if (CI.getFrontendOpts().RelocatablePCH)
84 1: return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
85 :
86 43: return CreatePCHGenerator(CI.getPreprocessor(), OS);
87 : }
88 :
89 : ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
90 3: llvm::StringRef InFile) {
3: branch 2 taken
0: branch 3 not taken
91 3: if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
92 3: return CreateHTMLPrinter(OS, CI.getPreprocessor());
93 0: return 0;
94 : }
95 :
96 : ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
97 0: llvm::StringRef InFile) {
98 0: return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
99 : }
100 :
101 12: FixItAction::FixItAction() {}
12: branch 2 taken
0: branch 3 not taken
0: branch 7 not taken
0: branch 8 not taken
0: branch 12 not taken
0: branch 13 not taken
102 12: FixItAction::~FixItAction() {}
103 :
104 : ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
105 12: llvm::StringRef InFile) {
106 12: 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 12: FixItRewriter &FixItRewrite) {
113 : const std::vector<ParsedSourceLocation> &Locs =
114 12: CI.getFrontendOpts().FixItLocations;
0: branch 1 not taken
12: branch 2 taken
115 12: for (unsigned i = 0, e = Locs.size(); i != e; ++i) {
116 0: const FileEntry *File = CI.getFileManager().getFile(Locs[i].FileName);
0: branch 0 not taken
0: branch 1 not taken
117 0: if (!File) {
118 : CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file)
119 0: << Locs[i].FileName;
120 0: return false;
121 : }
122 :
123 : RequestedSourceLocation Requested;
124 0: Requested.File = File;
125 0: Requested.Line = Locs[i].Line;
126 0: Requested.Column = Locs[i].Column;
127 0: FixItRewrite.addFixItLocation(Requested);
128 : }
129 :
130 12: return true;
131 : }
132 :
133 : bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
134 12: llvm::StringRef Filename) {
135 : Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
136 12: CI.getLangOpts()));
0: branch 2 not taken
12: branch 3 taken
137 12: if (!AddFixItLocations(CI, *Rewriter))
138 0: return false;
139 :
140 12: return true;
141 : }
142 :
143 12: void FixItAction::EndSourceFileAction() {
144 12: const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
145 12: Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile);
146 12: }
147 :
148 : ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
149 51: llvm::StringRef InFile) {
51: branch 2 taken
0: branch 3 not taken
150 51: if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
151 : return CreateObjCRewriter(InFile, OS,
152 : CI.getDiagnostics(), CI.getLangOpts(),
153 51: CI.getDiagnosticOpts().NoRewriteMacros);
154 0: return 0;
155 : }
156 :
157 : ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
158 1280: llvm::StringRef InFile) {
159 1280: return new ASTConsumer();
160 : }
161 :
162 626: CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {}
163 :
164 : ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
165 626: llvm::StringRef InFile) {
166 626: BackendAction BA = static_cast<BackendAction>(Act);
167 626: llvm::OwningPtr<llvm::raw_ostream> OS;
57: branch 0 taken
516: branch 1 taken
18: branch 2 taken
35: branch 3 taken
0: branch 4 not taken
0: branch 5 not taken
168 626: switch (BA) {
169 : case Backend_EmitAssembly:
170 57: OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
171 57: break;
172 : case Backend_EmitLL:
173 516: OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
174 516: break;
175 : case Backend_EmitBC:
176 18: OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
177 18: break;
178 : case Backend_EmitNothing:
179 35: break;
180 : case Backend_EmitObj:
181 0: OS.reset(CI.createDefaultOutputFile(true, InFile, "o"));
182 : break;
183 : }
591: branch 0 taken
35: branch 1 taken
1: branch 3 taken
590: branch 4 taken
1: branch 5 taken
625: branch 6 taken
184 626: if (BA != Backend_EmitNothing && !OS)
185 1: return 0;
186 :
187 : return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
188 : CI.getCodeGenOpts(), CI.getTargetOpts(),
189 : CI.getFrontendOpts().ShowTimers, InFile,
190 625: OS.take(), CI.getLLVMContext());
191 : }
192 :
193 57: EmitAssemblyAction::EmitAssemblyAction()
194 57: : CodeGenAction(Backend_EmitAssembly) {}
195 :
196 18: EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {}
197 :
198 516: EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {}
199 :
200 35: EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {}
201 :
202 0: EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {}
203 :
204 : //===----------------------------------------------------------------------===//
205 : // Preprocessor Actions
206 : //===----------------------------------------------------------------------===//
207 :
208 0: void DumpRawTokensAction::ExecuteAction() {
209 0: Preprocessor &PP = getCompilerInstance().getPreprocessor();
210 0: SourceManager &SM = PP.getSourceManager();
211 :
212 : // Start lexing the specified input file.
213 0: const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
214 0: Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
215 0: RawLex.SetKeepWhitespaceMode(true);
216 :
217 0: Token RawTok;
218 0: RawLex.LexFromRawLexer(RawTok);
0: branch 1 not taken
0: branch 2 not taken
219 0: while (RawTok.isNot(tok::eof)) {
220 0: PP.DumpToken(RawTok, true);
221 0: llvm::errs() << "\n";
222 0: RawLex.LexFromRawLexer(RawTok);
223 0: }
224 0: }
225 :
226 3: void DumpTokensAction::ExecuteAction() {
227 3: Preprocessor &PP = getCompilerInstance().getPreprocessor();
228 : // Start preprocessing the specified input file.
229 3: Token Tok;
230 3: PP.EnterMainSourceFile();
34: branch 1 taken
3: branch 2 taken
231 37: do {
232 37: PP.Lex(Tok);
233 37: PP.DumpToken(Tok, true);
234 37: llvm::errs() << "\n";
235 : } while (Tok.isNot(tok::eof));
236 3: }
237 :
238 1: void GeneratePTHAction::ExecuteAction() {
239 1: CompilerInstance &CI = getCompilerInstance();
1: branch 2 taken
0: branch 3 not taken
0: branch 6 not taken
1: branch 7 taken
0: branch 8 not taken
1: branch 9 taken
240 1: if (CI.getFrontendOpts().OutputFile.empty() ||
241 : CI.getFrontendOpts().OutputFile == "-") {
242 : // FIXME: Don't fail this way.
243 : // FIXME: Verify that we can actually seek in the given file.
244 0: llvm::llvm_report_error("PTH requires a seekable file for output!");
245 : }
246 : llvm::raw_fd_ostream *OS =
247 1: CI.createDefaultOutputFile(true, getCurrentFile());
1: branch 0 taken
0: branch 1 not taken
248 1: if (!OS) return;
249 :
250 1: CacheTokens(CI.getPreprocessor(), OS);
251 : }
252 :
253 13: void ParseOnlyAction::ExecuteAction() {
254 13: Preprocessor &PP = getCompilerInstance().getPreprocessor();
255 13: llvm::OwningPtr<Action> PA(new MinimalAction(PP));
256 :
257 13: Parser P(PP, *PA);
258 13: PP.EnterMainSourceFile();
259 13: P.ParseTranslationUnit();
260 13: }
261 :
262 18: void PreprocessOnlyAction::ExecuteAction() {
263 18: Preprocessor &PP = getCompilerInstance().getPreprocessor();
264 :
265 18: Token Tok;
266 : // Start parsing the specified input file.
267 18: PP.EnterMainSourceFile();
148: branch 1 taken
18: branch 2 taken
268 166: do {
269 166: PP.Lex(Tok);
270 : } while (Tok.isNot(tok::eof));
271 18: }
272 :
273 2: void PrintParseAction::ExecuteAction() {
274 2: CompilerInstance &CI = getCompilerInstance();
275 2: Preprocessor &PP = getCompilerInstance().getPreprocessor();
276 2: llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
2: branch 0 taken
0: branch 1 not taken
277 2: if (!OS) return;
278 :
279 2: llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
280 :
281 2: Parser P(PP, *PA);
282 2: PP.EnterMainSourceFile();
283 2: P.ParseTranslationUnit();
284 : }
285 :
286 245: void PrintPreprocessedAction::ExecuteAction() {
287 245: CompilerInstance &CI = getCompilerInstance();
288 : // Output file needs to be set to 'Binary', to avoid converting Unix style
289 : // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
290 245: llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
245: branch 0 taken
0: branch 1 not taken
291 245: if (!OS) return;
292 :
293 : DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
294 245: CI.getPreprocessorOutputOpts());
295 : }
296 :
297 1: void RewriteMacrosAction::ExecuteAction() {
298 1: CompilerInstance &CI = getCompilerInstance();
299 1: llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
1: branch 0 taken
0: branch 1 not taken
300 1: if (!OS) return;
301 :
302 1: RewriteMacrosInInput(CI.getPreprocessor(), OS);
303 : }
304 :
305 0: void RewriteTestAction::ExecuteAction() {
306 0: CompilerInstance &CI = getCompilerInstance();
307 0: llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
0: branch 0 not taken
0: branch 1 not taken
308 0: if (!OS) return;
309 :
310 0: DoRewriteTest(CI.getPreprocessor(), OS);
311 : }
Generated: 2010-02-10 01:31 by zcov