blob: be9bab16be1166af8b9252125db6c56bd4ff4b51 [file] [log] [blame]
Daniel Dunbarb3375cb2009-11-17 06:02:29 +00001//===--- CompilerInvocation.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/CompilerInvocation.h"
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/Support/ErrorHandling.h"
13using namespace clang;
14
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000015static const char *getAnalysisName(Analyses Kind) {
16 switch (Kind) {
17 default:
18 llvm::llvm_unreachable("Unknown analysis store!");
19#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
Daniel Dunbare2814d82009-11-22 22:08:20 +000020 case NAME: return "-" CMDFLAG;
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000021#include "clang/Frontend/Analyses.def"
22 }
23}
24
25static const char *getAnalysisStoreName(AnalysisStores Kind) {
26 switch (Kind) {
27 default:
28 llvm::llvm_unreachable("Unknown analysis store!");
29#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) \
30 case NAME##Model: return CMDFLAG;
31#include "clang/Frontend/Analyses.def"
32 }
33}
34
35static const char *getAnalysisConstraintName(AnalysisConstraints Kind) {
36 switch (Kind) {
37 default:
38 llvm::llvm_unreachable("Unknown analysis constraints!");
39#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) \
40 case NAME##Model: return CMDFLAG;
41#include "clang/Frontend/Analyses.def"
42 }
43}
44
45static const char *getAnalysisDiagClientName(AnalysisDiagClients Kind) {
46 switch (Kind) {
47 default:
48 llvm::llvm_unreachable("Unknown analysis client!");
49#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE) \
50 case PD_##NAME: return CMDFLAG;
51#include "clang/Frontend/Analyses.def"
52 }
53}
54
55static void AnalyzerOptsToArgs(const AnalyzerOptions &Opts,
56 std::vector<std::string> &Res) {
57 for (unsigned i = 0, e = Opts.AnalysisList.size(); i != e; ++i)
58 Res.push_back(getAnalysisName(Opts.AnalysisList[i]));
59 if (Opts.AnalysisStoreOpt != BasicStoreModel) {
60 Res.push_back("-analyzer-store");
61 Res.push_back(getAnalysisStoreName(Opts.AnalysisStoreOpt));
62 }
63 if (Opts.AnalysisConstraintsOpt != RangeConstraintsModel) {
64 Res.push_back("-analyzer-constraints");
65 Res.push_back(getAnalysisConstraintName(Opts.AnalysisConstraintsOpt));
66 }
67 if (Opts.AnalysisDiagOpt != PD_HTML) {
68 Res.push_back("-analyzer-output");
69 Res.push_back(getAnalysisDiagClientName(Opts.AnalysisDiagOpt));
70 }
71 if (!Opts.AnalyzeSpecificFunction.empty()) {
72 Res.push_back("-analyze-function");
73 Res.push_back(Opts.AnalyzeSpecificFunction);
74 }
75 if (Opts.AnalyzeAll)
76 Res.push_back("-analyzer-opt-analyze-headers");
77 if (Opts.AnalyzerDisplayProgress)
78 Res.push_back("-analyzer-display-progress");
79 if (Opts.EagerlyAssume)
80 Res.push_back("-analyzer-eagerly-assume");
Daniel Dunbar4db166b2009-11-19 05:32:09 +000081 if (!Opts.PurgeDead)
82 Res.push_back("-analyzer-no-purge-dead");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000083 if (Opts.TrimGraph)
84 Res.push_back("-trim-egraph");
85 if (Opts.VisualizeEGDot)
86 Res.push_back("-analyzer-viz-egraph-graphviz");
87 if (Opts.VisualizeEGDot)
88 Res.push_back("-analyzer-viz-egraph-ubigraph");
89 if (Opts.EnableExperimentalChecks)
90 Res.push_back("-analyzer-experimental-checks");
91 if (Opts.EnableExperimentalInternalChecks)
Daniel Dunbare2814d82009-11-22 22:08:20 +000092 Res.push_back("-analyzer-experimental-internal-checks");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000093}
94
95static void CodeGenOptsToArgs(const CodeGenOptions &Opts,
96 std::vector<std::string> &Res) {
97 if (Opts.DebugInfo)
98 Res.push_back("-g");
99 if (Opts.DisableLLVMOpts)
100 Res.push_back("-disable-llvm-optzns");
101 if (Opts.DisableRedZone)
102 Res.push_back("-disable-red-zone");
103 if (!Opts.MergeAllConstants)
104 Res.push_back("-fno-merge-all-constants");
Daniel Dunbar50a44872009-11-20 17:23:30 +0000105 if (Opts.NoCommon)
106 Res.push_back("-fno-common");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000107 if (Opts.NoImplicitFloat)
108 Res.push_back("-no-implicit-float");
109 if (Opts.OptimizeSize) {
110 assert(Opts.OptimizationLevel == 2 && "Invalid options!");
111 Res.push_back("-Os");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000112 } else if (Opts.OptimizationLevel != 0)
113 Res.push_back("-O" + llvm::utostr(Opts.OptimizationLevel));
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000114 // SimplifyLibCalls is only derived.
115 // TimePasses is only derived.
116 // UnitAtATime is unused.
117 // UnrollLoops is only derived.
118 // VerifyModule is only derived.
119 // Inlining is only derived.
120}
121
122static void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts,
123 std::vector<std::string> &Res) {
124 if (Opts.IncludeSystemHeaders)
125 Res.push_back("-sys-header-deps");
126 if (Opts.UsePhonyTargets)
127 Res.push_back("-MP");
128 if (!Opts.OutputFile.empty()) {
129 Res.push_back("-dependency-file");
130 Res.push_back(Opts.OutputFile);
131 }
132 for (unsigned i = 0, e = Opts.Targets.size(); i != e; ++i) {
133 Res.push_back("-MT");
134 Res.push_back(Opts.Targets[i]);
135 }
136}
137
138static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
139 std::vector<std::string> &Res) {
140 if (Opts.IgnoreWarnings)
141 Res.push_back("-w");
142 if (Opts.NoRewriteMacros)
143 Res.push_back("-Wno-rewrite-macros");
144 if (Opts.Pedantic)
145 Res.push_back("-pedantic");
146 if (Opts.PedanticErrors)
147 Res.push_back("-pedantic-errors");
148 if (!Opts.ShowColumn)
149 Res.push_back("-fno-show-column");
150 if (!Opts.ShowLocation)
151 Res.push_back("-fno-show-source-location");
152 if (!Opts.ShowCarets)
153 Res.push_back("-fno-caret-diagnostics");
154 if (!Opts.ShowFixits)
155 Res.push_back("-fno-diagnostics-fixit-info");
156 if (Opts.ShowSourceRanges)
157 Res.push_back("-fdiagnostics-print-source-range-info");
158 if (Opts.ShowColors)
159 Res.push_back("-fcolor-diagnostics");
160 if (Opts.VerifyDiagnostics)
161 Res.push_back("-verify");
162 if (Opts.ShowOptionNames)
163 Res.push_back("-fdiagnostics-show-option");
164 if (Opts.MessageLength) {
165 Res.push_back("-fmessage-length");
166 Res.push_back(llvm::utostr(Opts.MessageLength));
167 }
168 if (!Opts.DumpBuildInformation.empty()) {
169 Res.push_back("-dump-build-information");
170 Res.push_back(Opts.DumpBuildInformation);
171 }
172 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i)
173 Res.push_back("-W" + Opts.Warnings[i]);
174}
175
176static const char *getInputKindName(FrontendOptions::InputKind Kind) {
177 switch (Kind) {
178 case FrontendOptions::IK_None: break;
179 case FrontendOptions::IK_AST: return "ast";
180 case FrontendOptions::IK_Asm: return "assembler-with-cpp";
181 case FrontendOptions::IK_C: return "c";
182 case FrontendOptions::IK_CXX: return "c++";
183 case FrontendOptions::IK_ObjC: return "objective-c";
184 case FrontendOptions::IK_ObjCXX: return "objective-c++";
185 case FrontendOptions::IK_OpenCL: return "cl";
186 case FrontendOptions::IK_PreprocessedC: return "cpp-output";
187 case FrontendOptions::IK_PreprocessedCXX: return "c++-cpp-output";
188 case FrontendOptions::IK_PreprocessedObjC: return "objective-c-cpp-output";
189 case FrontendOptions::IK_PreprocessedObjCXX: return "objective-c++-cpp-output";
190 }
191
192 llvm::llvm_unreachable("Unexpected language kind!");
193 return 0;
194}
195
196static const char *getActionName(frontend::ActionKind Kind) {
197 switch (Kind) {
198 case frontend::PluginAction:
199 case frontend::InheritanceView:
200 llvm::llvm_unreachable("Invalid kind!");
201
202 case frontend::ASTDump: return "-ast-dump";
203 case frontend::ASTPrint: return "-ast-print";
204 case frontend::ASTPrintXML: return "-ast-print-xml";
205 case frontend::ASTView: return "-ast-view";
206 case frontend::DumpRawTokens: return "-dump-raw-tokens";
207 case frontend::DumpRecordLayouts: return "-dump-record-layouts";
208 case frontend::DumpTokens: return "-dump-tokens";
209 case frontend::EmitAssembly: return "-S";
210 case frontend::EmitBC: return "-emit-llvm-bc";
211 case frontend::EmitHTML: return "-emit-html";
212 case frontend::EmitLLVM: return "-emit-llvm";
213 case frontend::EmitLLVMOnly: return "-emit-llvm-only";
214 case frontend::FixIt: return "-fixit";
215 case frontend::GeneratePCH: return "-emit-pch";
216 case frontend::GeneratePTH: return "-emit-pth";
217 case frontend::ParseNoop: return "-parse-noop";
218 case frontend::ParsePrintCallbacks: return "-parse-print-callbacks";
219 case frontend::ParseSyntaxOnly: return "-fsyntax-only";
220 case frontend::PrintDeclContext: return "-print-decl-contexts";
221 case frontend::PrintPreprocessedInput: return "-E";
222 case frontend::RewriteBlocks: return "-rewrite-blocks";
223 case frontend::RewriteMacros: return "-rewrite-macros";
224 case frontend::RewriteObjC: return "-rewrite-objc";
225 case frontend::RewriteTest: return "-rewrite-test";
226 case frontend::RunAnalysis: return "-analyze";
227 case frontend::RunPreprocessorOnly: return "-Eonly";
228 }
229
230 llvm::llvm_unreachable("Unexpected language kind!");
231 return 0;
232}
233
234static void FrontendOptsToArgs(const FrontendOptions &Opts,
235 std::vector<std::string> &Res) {
236 if (!Opts.DebugCodeCompletionPrinter)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000237 Res.push_back("-no-code-completion-debug-printer");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000238 if (Opts.DisableFree)
239 Res.push_back("-disable-free");
240 if (Opts.EmptyInputOnly)
241 Res.push_back("-empty-input-only");
242 if (Opts.RelocatablePCH)
243 Res.push_back("-relocatable-pch");
244 if (Opts.ShowMacrosInCodeCompletion)
245 Res.push_back("-code-completion-macros");
246 if (Opts.ShowStats)
Daniel Dunbareb01ac82009-11-25 10:14:52 +0000247 Res.push_back("-print-stats");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000248 if (Opts.ShowTimers)
249 Res.push_back("-ftime-report");
250
251 bool NeedLang = false;
252 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i)
253 if (FrontendOptions::getInputKindForExtension(Opts.Inputs[i].second) !=
254 Opts.Inputs[i].first)
255 NeedLang = true;
256 if (NeedLang) {
257 Res.push_back("-x");
258 Res.push_back(getInputKindName(Opts.Inputs[0].first));
259 }
260 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i) {
261 assert((!NeedLang || Opts.Inputs[i].first == Opts.Inputs[0].first) &&
262 "Unable to represent this input vector!");
263 Res.push_back(Opts.Inputs[i].second);
264 }
265
266 if (!Opts.OutputFile.empty()) {
267 Res.push_back("-o");
268 Res.push_back(Opts.OutputFile);
269 }
270 if (!Opts.ViewClassInheritance.empty()) {
271 Res.push_back("-cxx-inheritance-view");
272 Res.push_back(Opts.ViewClassInheritance);
273 }
274 for (unsigned i = 0, e = Opts.FixItLocations.size(); i != e; ++i) {
275 Res.push_back("-fixit-at");
276 Res.push_back(Opts.FixItLocations[i].FileName + ":" +
277 llvm::utostr(Opts.FixItLocations[i].Line) + ":" +
278 llvm::utostr(Opts.FixItLocations[i].Column));
279 }
280 if (!Opts.CodeCompletionAt.FileName.empty()) {
281 Res.push_back("-code-completion-at");
282 Res.push_back(Opts.CodeCompletionAt.FileName + ":" +
283 llvm::utostr(Opts.CodeCompletionAt.Line) + ":" +
284 llvm::utostr(Opts.CodeCompletionAt.Column));
285 }
286 if (Opts.ProgramAction != frontend::InheritanceView &&
287 Opts.ProgramAction != frontend::PluginAction)
288 Res.push_back(getActionName(Opts.ProgramAction));
289 if (!Opts.ActionName.empty()) {
290 Res.push_back("-plugin");
291 Res.push_back(Opts.ActionName);
292 }
293}
294
295static void HeaderSearchOptsToArgs(const HeaderSearchOptions &Opts,
296 std::vector<std::string> &Res) {
Daniel Dunbarc59dc922009-11-19 05:32:21 +0000297 if (Opts.Sysroot != "/") {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000298 Res.push_back("-isysroot");
299 Res.push_back(Opts.Sysroot);
300 }
301
302 /// User specified include entries.
303 for (unsigned i = 0, e = Opts.UserEntries.size(); i != e; ++i) {
304 const HeaderSearchOptions::Entry &E = Opts.UserEntries[i];
Daniel Dunbard8c78062009-11-26 02:13:54 +0000305 if (E.IsFramework && (E.Group != frontend::Angled || !E.IsUserSupplied))
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000306 llvm::llvm_report_error("Invalid option set!");
307 if (E.IsUserSupplied) {
308 if (E.Group == frontend::After) {
309 Res.push_back("-idirafter");
310 } else if (E.Group == frontend::Quoted) {
Daniel Dunbard8c78062009-11-26 02:13:54 +0000311 Res.push_back("-iquote");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000312 } else if (E.Group == frontend::System) {
313 Res.push_back("-isystem");
314 } else {
315 assert(E.Group == frontend::Angled && "Invalid group!");
316 Res.push_back(E.IsFramework ? "-F" : "-I");
317 }
318 } else {
319 if (E.Group != frontend::Angled && E.Group != frontend::System)
320 llvm::llvm_report_error("Invalid option set!");
321 Res.push_back(E.Group == frontend::Angled ? "-iwithprefixbefore" :
322 "-iwithprefix");
323 }
324 Res.push_back(E.Path);
325 }
326
327 if (!Opts.EnvIncPath.empty()) {
328 // FIXME: Provide an option for this, and move env detection to driver.
329 llvm::llvm_report_error("Not yet implemented!");
330 }
331 if (!Opts.CEnvIncPath.empty()) {
332 // FIXME: Provide an option for this, and move env detection to driver.
333 llvm::llvm_report_error("Not yet implemented!");
334 }
335 if (!Opts.ObjCEnvIncPath.empty()) {
336 // FIXME: Provide an option for this, and move env detection to driver.
337 llvm::llvm_report_error("Not yet implemented!");
338 }
339 if (!Opts.CXXEnvIncPath.empty()) {
340 // FIXME: Provide an option for this, and move env detection to driver.
341 llvm::llvm_report_error("Not yet implemented!");
342 }
343 if (!Opts.ObjCXXEnvIncPath.empty()) {
344 // FIXME: Provide an option for this, and move env detection to driver.
345 llvm::llvm_report_error("Not yet implemented!");
346 }
347 if (!Opts.BuiltinIncludePath.empty()) {
348 // FIXME: Provide an option for this, and move to driver.
349 }
350 if (!Opts.UseStandardIncludes)
351 Res.push_back("-nostdinc");
352 if (Opts.Verbose)
353 Res.push_back("-v");
354}
355
356static void LangOptsToArgs(const LangOptions &Opts,
357 std::vector<std::string> &Res) {
358 LangOptions DefaultLangOpts;
359
360 // FIXME: Need to set -std to get all the implicit options.
361
362 // FIXME: We want to only pass options relative to the defaults, which
363 // requires constructing a target. :(
364 //
365 // It would be better to push the all target specific choices into the driver,
366 // so that everything below that was more uniform.
367
368 if (Opts.Trigraphs)
369 Res.push_back("-trigraphs");
370 // Implicit based on the input kind:
371 // AsmPreprocessor, CPlusPlus, ObjC1, ObjC2, OpenCL
372 // Implicit based on the input language standard:
373 // BCPLComment, C99, CPlusPlus0x, Digraphs, GNUInline, ImplicitInt, GNUMode
374 if (Opts.DollarIdents)
375 Res.push_back("-fdollars-in-identifiers");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000376 if (Opts.Microsoft)
377 Res.push_back("-fms-extensions=1");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000378 if (Opts.ObjCNonFragileABI)
379 Res.push_back("-fobjc-nonfragile-abi");
380 // NoInline is implicit.
381 if (!Opts.CXXOperatorNames)
382 Res.push_back("-fno-operator-names");
383 if (Opts.PascalStrings)
384 Res.push_back("-fpascal-strings");
385 if (Opts.WritableStrings)
386 Res.push_back("-fwritable-strings");
387 if (!Opts.LaxVectorConversions)
388 Res.push_back("-fno-lax-vector-conversions");
389 if (Opts.AltiVec)
390 Res.push_back("-faltivec");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000391 if (Opts.Exceptions)
392 Res.push_back("-fexceptions");
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000393 if (!Opts.Rtti)
394 Res.push_back("-fno-rtti");
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +0000395 if (!Opts.NeXTRuntime)
396 Res.push_back("-fgnu-runtime");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000397 if (Opts.Freestanding)
398 Res.push_back("-ffreestanding");
399 if (Opts.NoBuiltin)
400 Res.push_back("-fno-builtin");
401 if (Opts.ThreadsafeStatics)
402 llvm::llvm_report_error("FIXME: Not yet implemented!");
403 if (Opts.POSIXThreads)
404 Res.push_back("-pthread");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000405 if (Opts.Blocks)
406 Res.push_back("-fblocks=1");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000407 if (Opts.EmitAllDecls)
408 Res.push_back("-femit-all-decls");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000409 if (!Opts.MathErrno)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000410 Res.push_back("-fno-math-errno");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000411 if (Opts.OverflowChecking)
412 Res.push_back("-ftrapv");
413 if (Opts.HeinousExtensions)
414 Res.push_back("-fheinous-gnu-extensions");
415 // Optimize is implicit.
416 // OptimizeSize is implicit.
417 if (Opts.Static)
418 Res.push_back("-static-define");
419 if (Opts.PICLevel) {
420 Res.push_back("-pic-level");
421 Res.push_back(llvm::utostr(Opts.PICLevel));
422 }
423 if (Opts.ObjCGCBitmapPrint)
424 Res.push_back("-print-ivar-layout");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000425 // FIXME: Don't forget to update when the default changes!
426 if (Opts.AccessControl)
427 Res.push_back("-faccess-control");
428 if (!Opts.CharIsSigned)
429 Res.push_back("-fsigned-char=0");
430 if (Opts.ShortWChar)
431 Res.push_back("-fshort-wchar");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000432 if (!Opts.ElideConstructors)
433 Res.push_back("-fno-elide-constructors");
434 if (Opts.getGCMode() != LangOptions::NonGC) {
435 if (Opts.getGCMode() == LangOptions::HybridGC) {
436 Res.push_back("-fobjc-gc");
437 } else {
438 assert(Opts.getGCMode() == LangOptions::GCOnly && "Invalid GC mode!");
439 Res.push_back("-fobjc-gc-only");
440 }
441 }
442 if (Opts.getVisibilityMode() != LangOptions::Default) {
443 Res.push_back("-fvisibility");
444 if (Opts.getVisibilityMode() == LangOptions::Hidden) {
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000445 Res.push_back("hidden");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000446 } else {
447 assert(Opts.getVisibilityMode() == LangOptions::Protected &&
448 "Invalid visibility!");
449 Res.push_back("protected");
450 }
451 }
452 if (Opts.getStackProtectorMode() != 0) {
453 Res.push_back("-stack-protector");
454 Res.push_back(llvm::utostr(Opts.getStackProtectorMode()));
455 }
456 if (Opts.getMainFileName()) {
457 Res.push_back("-main-file-name");
458 Res.push_back(Opts.getMainFileName());
459 }
460 if (Opts.InstantiationDepth != DefaultLangOpts.InstantiationDepth) {
461 Res.push_back("-ftemplate-depth");
462 Res.push_back(llvm::utostr(Opts.InstantiationDepth));
463 }
464 if (Opts.ObjCConstantStringClass) {
465 Res.push_back("-fconstant-string-class");
466 Res.push_back(Opts.ObjCConstantStringClass);
467 }
468}
469
470static void PreprocessorOptsToArgs(const PreprocessorOptions &Opts,
471 std::vector<std::string> &Res) {
472 for (unsigned i = 0, e = Opts.Macros.size(); i != e; ++i)
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000473 Res.push_back(std::string(Opts.Macros[i].second ? "-U" : "-D") +
474 Opts.Macros[i].first);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000475 for (unsigned i = 0, e = Opts.Includes.size(); i != e; ++i) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000476 // FIXME: We need to avoid reincluding the implicit PCH and PTH includes.
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000477 Res.push_back("-include");
478 Res.push_back(Opts.Includes[i]);
479 }
480 for (unsigned i = 0, e = Opts.MacroIncludes.size(); i != e; ++i) {
481 Res.push_back("-imacros");
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000482 Res.push_back(Opts.MacroIncludes[i]);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000483 }
484 if (!Opts.UsePredefines)
485 Res.push_back("-undef");
486 if (!Opts.ImplicitPCHInclude.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000487 Res.push_back("-include-pch");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000488 Res.push_back(Opts.ImplicitPCHInclude);
489 }
490 if (!Opts.ImplicitPTHInclude.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000491 Res.push_back("-include-pth");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000492 Res.push_back(Opts.ImplicitPTHInclude);
493 }
494 if (!Opts.TokenCache.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000495 if (Opts.ImplicitPTHInclude.empty()) {
496 Res.push_back("-token-cache");
497 Res.push_back(Opts.TokenCache);
498 } else
499 assert(Opts.ImplicitPTHInclude == Opts.TokenCache &&
500 "Unsupported option combination!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000501 }
502}
503
504static void PreprocessorOutputOptsToArgs(const PreprocessorOutputOptions &Opts,
505 std::vector<std::string> &Res) {
506 if (!Opts.ShowCPP && !Opts.ShowMacros)
507 llvm::llvm_report_error("Invalid option combination!");
508
509 if (Opts.ShowCPP && Opts.ShowMacros)
510 Res.push_back("-dD");
511 else if (!Opts.ShowCPP && Opts.ShowMacros)
512 Res.push_back("-dM");
513
514 if (!Opts.ShowLineMarkers)
515 Res.push_back("-P");
516 if (Opts.ShowComments)
517 Res.push_back("-C");
518 if (Opts.ShowMacroComments)
519 Res.push_back("-CC");
520}
521
522static void TargetOptsToArgs(const TargetOptions &Opts,
523 std::vector<std::string> &Res) {
524 Res.push_back("-triple");
525 Res.push_back(Opts.Triple);
526 if (!Opts.CPU.empty()) {
Daniel Dunbar21affc02009-11-23 23:41:17 +0000527 Res.push_back("-mcpu");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000528 Res.push_back(Opts.CPU);
529 }
530 if (!Opts.ABI.empty()) {
531 Res.push_back("-target-abi");
532 Res.push_back(Opts.ABI);
533 }
534 for (unsigned i = 0, e = Opts.Features.size(); i != e; ++i) {
535 Res.push_back("-target-feature");
536 Res.push_back(Opts.Features[i]);
537 }
538}
539
540void CompilerInvocation::toArgs(std::vector<std::string> &Res) {
541 AnalyzerOptsToArgs(getAnalyzerOpts(), Res);
542 CodeGenOptsToArgs(getCodeGenOpts(), Res);
543 DependencyOutputOptsToArgs(getDependencyOutputOpts(), Res);
544 DiagnosticOptsToArgs(getDiagnosticOpts(), Res);
545 FrontendOptsToArgs(getFrontendOpts(), Res);
546 HeaderSearchOptsToArgs(getHeaderSearchOpts(), Res);
547 LangOptsToArgs(getLangOpts(), Res);
548 PreprocessorOptsToArgs(getPreprocessorOpts(), Res);
549 PreprocessorOutputOptsToArgs(getPreprocessorOutputOpts(), Res);
550 TargetOptsToArgs(getTargetOpts(), Res);
551}