blob: 705cfef4e0619aaf1df7e92714b3b97f05a47a74 [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 Dunbar7d065d02009-11-29 02:38:34 +0000114 if (!Opts.MainFileName.empty()) {
115 Res.push_back("-main-file-name");
116 Res.push_back(Opts.MainFileName);
117 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000118 // SimplifyLibCalls is only derived.
119 // TimePasses is only derived.
120 // UnitAtATime is unused.
121 // UnrollLoops is only derived.
122 // VerifyModule is only derived.
123 // Inlining is only derived.
124}
125
126static void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts,
127 std::vector<std::string> &Res) {
128 if (Opts.IncludeSystemHeaders)
129 Res.push_back("-sys-header-deps");
130 if (Opts.UsePhonyTargets)
131 Res.push_back("-MP");
132 if (!Opts.OutputFile.empty()) {
133 Res.push_back("-dependency-file");
134 Res.push_back(Opts.OutputFile);
135 }
136 for (unsigned i = 0, e = Opts.Targets.size(); i != e; ++i) {
137 Res.push_back("-MT");
138 Res.push_back(Opts.Targets[i]);
139 }
140}
141
142static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
143 std::vector<std::string> &Res) {
144 if (Opts.IgnoreWarnings)
145 Res.push_back("-w");
146 if (Opts.NoRewriteMacros)
147 Res.push_back("-Wno-rewrite-macros");
148 if (Opts.Pedantic)
149 Res.push_back("-pedantic");
150 if (Opts.PedanticErrors)
151 Res.push_back("-pedantic-errors");
152 if (!Opts.ShowColumn)
153 Res.push_back("-fno-show-column");
154 if (!Opts.ShowLocation)
155 Res.push_back("-fno-show-source-location");
156 if (!Opts.ShowCarets)
157 Res.push_back("-fno-caret-diagnostics");
158 if (!Opts.ShowFixits)
159 Res.push_back("-fno-diagnostics-fixit-info");
160 if (Opts.ShowSourceRanges)
161 Res.push_back("-fdiagnostics-print-source-range-info");
162 if (Opts.ShowColors)
163 Res.push_back("-fcolor-diagnostics");
164 if (Opts.VerifyDiagnostics)
165 Res.push_back("-verify");
166 if (Opts.ShowOptionNames)
167 Res.push_back("-fdiagnostics-show-option");
168 if (Opts.MessageLength) {
169 Res.push_back("-fmessage-length");
170 Res.push_back(llvm::utostr(Opts.MessageLength));
171 }
172 if (!Opts.DumpBuildInformation.empty()) {
173 Res.push_back("-dump-build-information");
174 Res.push_back(Opts.DumpBuildInformation);
175 }
176 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i)
177 Res.push_back("-W" + Opts.Warnings[i]);
178}
179
180static const char *getInputKindName(FrontendOptions::InputKind Kind) {
181 switch (Kind) {
Daniel Dunbar1b096952009-11-29 02:38:47 +0000182 case FrontendOptions::IK_None: break;
183 case FrontendOptions::IK_AST: return "ast";
184 case FrontendOptions::IK_Asm: return "assembler-with-cpp";
185 case FrontendOptions::IK_C: return "c";
186 case FrontendOptions::IK_CXX: return "c++";
187 case FrontendOptions::IK_ObjC: return "objective-c";
188 case FrontendOptions::IK_ObjCXX: return "objective-c++";
189 case FrontendOptions::IK_OpenCL: return "cl";
190 case FrontendOptions::IK_PreprocessedC: return "cpp-output";
191 case FrontendOptions::IK_PreprocessedCXX: return "c++-cpp-output";
192 case FrontendOptions::IK_PreprocessedObjC: return "objective-c-cpp-output";
193 case FrontendOptions::IK_PreprocessedObjCXX:return "objective-c++-cpp-output";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000194 }
195
196 llvm::llvm_unreachable("Unexpected language kind!");
197 return 0;
198}
199
200static const char *getActionName(frontend::ActionKind Kind) {
201 switch (Kind) {
202 case frontend::PluginAction:
203 case frontend::InheritanceView:
204 llvm::llvm_unreachable("Invalid kind!");
205
206 case frontend::ASTDump: return "-ast-dump";
207 case frontend::ASTPrint: return "-ast-print";
208 case frontend::ASTPrintXML: return "-ast-print-xml";
209 case frontend::ASTView: return "-ast-view";
210 case frontend::DumpRawTokens: return "-dump-raw-tokens";
211 case frontend::DumpRecordLayouts: return "-dump-record-layouts";
212 case frontend::DumpTokens: return "-dump-tokens";
213 case frontend::EmitAssembly: return "-S";
214 case frontend::EmitBC: return "-emit-llvm-bc";
215 case frontend::EmitHTML: return "-emit-html";
216 case frontend::EmitLLVM: return "-emit-llvm";
217 case frontend::EmitLLVMOnly: return "-emit-llvm-only";
218 case frontend::FixIt: return "-fixit";
219 case frontend::GeneratePCH: return "-emit-pch";
220 case frontend::GeneratePTH: return "-emit-pth";
221 case frontend::ParseNoop: return "-parse-noop";
222 case frontend::ParsePrintCallbacks: return "-parse-print-callbacks";
223 case frontend::ParseSyntaxOnly: return "-fsyntax-only";
224 case frontend::PrintDeclContext: return "-print-decl-contexts";
225 case frontend::PrintPreprocessedInput: return "-E";
226 case frontend::RewriteBlocks: return "-rewrite-blocks";
227 case frontend::RewriteMacros: return "-rewrite-macros";
228 case frontend::RewriteObjC: return "-rewrite-objc";
229 case frontend::RewriteTest: return "-rewrite-test";
230 case frontend::RunAnalysis: return "-analyze";
231 case frontend::RunPreprocessorOnly: return "-Eonly";
232 }
233
234 llvm::llvm_unreachable("Unexpected language kind!");
235 return 0;
236}
237
238static void FrontendOptsToArgs(const FrontendOptions &Opts,
239 std::vector<std::string> &Res) {
240 if (!Opts.DebugCodeCompletionPrinter)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000241 Res.push_back("-no-code-completion-debug-printer");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000242 if (Opts.DisableFree)
243 Res.push_back("-disable-free");
244 if (Opts.EmptyInputOnly)
245 Res.push_back("-empty-input-only");
246 if (Opts.RelocatablePCH)
247 Res.push_back("-relocatable-pch");
248 if (Opts.ShowMacrosInCodeCompletion)
249 Res.push_back("-code-completion-macros");
250 if (Opts.ShowStats)
Daniel Dunbareb01ac82009-11-25 10:14:52 +0000251 Res.push_back("-print-stats");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000252 if (Opts.ShowTimers)
253 Res.push_back("-ftime-report");
254
255 bool NeedLang = false;
256 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i)
257 if (FrontendOptions::getInputKindForExtension(Opts.Inputs[i].second) !=
258 Opts.Inputs[i].first)
259 NeedLang = true;
260 if (NeedLang) {
261 Res.push_back("-x");
262 Res.push_back(getInputKindName(Opts.Inputs[0].first));
263 }
264 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i) {
265 assert((!NeedLang || Opts.Inputs[i].first == Opts.Inputs[0].first) &&
266 "Unable to represent this input vector!");
267 Res.push_back(Opts.Inputs[i].second);
268 }
269
270 if (!Opts.OutputFile.empty()) {
271 Res.push_back("-o");
272 Res.push_back(Opts.OutputFile);
273 }
274 if (!Opts.ViewClassInheritance.empty()) {
275 Res.push_back("-cxx-inheritance-view");
276 Res.push_back(Opts.ViewClassInheritance);
277 }
278 for (unsigned i = 0, e = Opts.FixItLocations.size(); i != e; ++i) {
279 Res.push_back("-fixit-at");
280 Res.push_back(Opts.FixItLocations[i].FileName + ":" +
281 llvm::utostr(Opts.FixItLocations[i].Line) + ":" +
282 llvm::utostr(Opts.FixItLocations[i].Column));
283 }
284 if (!Opts.CodeCompletionAt.FileName.empty()) {
285 Res.push_back("-code-completion-at");
286 Res.push_back(Opts.CodeCompletionAt.FileName + ":" +
287 llvm::utostr(Opts.CodeCompletionAt.Line) + ":" +
288 llvm::utostr(Opts.CodeCompletionAt.Column));
289 }
290 if (Opts.ProgramAction != frontend::InheritanceView &&
291 Opts.ProgramAction != frontend::PluginAction)
292 Res.push_back(getActionName(Opts.ProgramAction));
293 if (!Opts.ActionName.empty()) {
294 Res.push_back("-plugin");
295 Res.push_back(Opts.ActionName);
296 }
297}
298
299static void HeaderSearchOptsToArgs(const HeaderSearchOptions &Opts,
300 std::vector<std::string> &Res) {
Daniel Dunbarc59dc922009-11-19 05:32:21 +0000301 if (Opts.Sysroot != "/") {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000302 Res.push_back("-isysroot");
303 Res.push_back(Opts.Sysroot);
304 }
305
306 /// User specified include entries.
307 for (unsigned i = 0, e = Opts.UserEntries.size(); i != e; ++i) {
308 const HeaderSearchOptions::Entry &E = Opts.UserEntries[i];
Daniel Dunbard8c78062009-11-26 02:13:54 +0000309 if (E.IsFramework && (E.Group != frontend::Angled || !E.IsUserSupplied))
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000310 llvm::llvm_report_error("Invalid option set!");
311 if (E.IsUserSupplied) {
312 if (E.Group == frontend::After) {
313 Res.push_back("-idirafter");
314 } else if (E.Group == frontend::Quoted) {
Daniel Dunbard8c78062009-11-26 02:13:54 +0000315 Res.push_back("-iquote");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000316 } else if (E.Group == frontend::System) {
317 Res.push_back("-isystem");
318 } else {
319 assert(E.Group == frontend::Angled && "Invalid group!");
320 Res.push_back(E.IsFramework ? "-F" : "-I");
321 }
322 } else {
323 if (E.Group != frontend::Angled && E.Group != frontend::System)
324 llvm::llvm_report_error("Invalid option set!");
325 Res.push_back(E.Group == frontend::Angled ? "-iwithprefixbefore" :
326 "-iwithprefix");
327 }
328 Res.push_back(E.Path);
329 }
330
331 if (!Opts.EnvIncPath.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.CEnvIncPath.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.ObjCEnvIncPath.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.CXXEnvIncPath.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.ObjCXXEnvIncPath.empty()) {
348 // FIXME: Provide an option for this, and move env detection to driver.
349 llvm::llvm_report_error("Not yet implemented!");
350 }
351 if (!Opts.BuiltinIncludePath.empty()) {
352 // FIXME: Provide an option for this, and move to driver.
353 }
354 if (!Opts.UseStandardIncludes)
355 Res.push_back("-nostdinc");
356 if (Opts.Verbose)
357 Res.push_back("-v");
358}
359
360static void LangOptsToArgs(const LangOptions &Opts,
361 std::vector<std::string> &Res) {
362 LangOptions DefaultLangOpts;
363
364 // FIXME: Need to set -std to get all the implicit options.
365
366 // FIXME: We want to only pass options relative to the defaults, which
367 // requires constructing a target. :(
368 //
369 // It would be better to push the all target specific choices into the driver,
370 // so that everything below that was more uniform.
371
372 if (Opts.Trigraphs)
373 Res.push_back("-trigraphs");
374 // Implicit based on the input kind:
375 // AsmPreprocessor, CPlusPlus, ObjC1, ObjC2, OpenCL
376 // Implicit based on the input language standard:
377 // BCPLComment, C99, CPlusPlus0x, Digraphs, GNUInline, ImplicitInt, GNUMode
378 if (Opts.DollarIdents)
379 Res.push_back("-fdollars-in-identifiers");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000380 if (Opts.Microsoft)
381 Res.push_back("-fms-extensions=1");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000382 if (Opts.ObjCNonFragileABI)
383 Res.push_back("-fobjc-nonfragile-abi");
384 // NoInline is implicit.
385 if (!Opts.CXXOperatorNames)
386 Res.push_back("-fno-operator-names");
387 if (Opts.PascalStrings)
388 Res.push_back("-fpascal-strings");
389 if (Opts.WritableStrings)
390 Res.push_back("-fwritable-strings");
391 if (!Opts.LaxVectorConversions)
392 Res.push_back("-fno-lax-vector-conversions");
393 if (Opts.AltiVec)
394 Res.push_back("-faltivec");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000395 if (Opts.Exceptions)
396 Res.push_back("-fexceptions");
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000397 if (!Opts.Rtti)
398 Res.push_back("-fno-rtti");
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +0000399 if (!Opts.NeXTRuntime)
400 Res.push_back("-fgnu-runtime");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000401 if (Opts.Freestanding)
402 Res.push_back("-ffreestanding");
403 if (Opts.NoBuiltin)
404 Res.push_back("-fno-builtin");
405 if (Opts.ThreadsafeStatics)
406 llvm::llvm_report_error("FIXME: Not yet implemented!");
407 if (Opts.POSIXThreads)
408 Res.push_back("-pthread");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000409 if (Opts.Blocks)
410 Res.push_back("-fblocks=1");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000411 if (Opts.EmitAllDecls)
412 Res.push_back("-femit-all-decls");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000413 if (!Opts.MathErrno)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000414 Res.push_back("-fno-math-errno");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000415 if (Opts.OverflowChecking)
416 Res.push_back("-ftrapv");
417 if (Opts.HeinousExtensions)
418 Res.push_back("-fheinous-gnu-extensions");
419 // Optimize is implicit.
420 // OptimizeSize is implicit.
421 if (Opts.Static)
422 Res.push_back("-static-define");
423 if (Opts.PICLevel) {
424 Res.push_back("-pic-level");
425 Res.push_back(llvm::utostr(Opts.PICLevel));
426 }
427 if (Opts.ObjCGCBitmapPrint)
428 Res.push_back("-print-ivar-layout");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000429 // FIXME: Don't forget to update when the default changes!
430 if (Opts.AccessControl)
431 Res.push_back("-faccess-control");
432 if (!Opts.CharIsSigned)
433 Res.push_back("-fsigned-char=0");
434 if (Opts.ShortWChar)
435 Res.push_back("-fshort-wchar");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000436 if (!Opts.ElideConstructors)
437 Res.push_back("-fno-elide-constructors");
438 if (Opts.getGCMode() != LangOptions::NonGC) {
439 if (Opts.getGCMode() == LangOptions::HybridGC) {
440 Res.push_back("-fobjc-gc");
441 } else {
442 assert(Opts.getGCMode() == LangOptions::GCOnly && "Invalid GC mode!");
443 Res.push_back("-fobjc-gc-only");
444 }
445 }
446 if (Opts.getVisibilityMode() != LangOptions::Default) {
447 Res.push_back("-fvisibility");
448 if (Opts.getVisibilityMode() == LangOptions::Hidden) {
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000449 Res.push_back("hidden");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000450 } else {
451 assert(Opts.getVisibilityMode() == LangOptions::Protected &&
452 "Invalid visibility!");
453 Res.push_back("protected");
454 }
455 }
456 if (Opts.getStackProtectorMode() != 0) {
457 Res.push_back("-stack-protector");
458 Res.push_back(llvm::utostr(Opts.getStackProtectorMode()));
459 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000460 if (Opts.InstantiationDepth != DefaultLangOpts.InstantiationDepth) {
461 Res.push_back("-ftemplate-depth");
462 Res.push_back(llvm::utostr(Opts.InstantiationDepth));
463 }
Daniel Dunbar1b096952009-11-29 02:38:47 +0000464 if (!Opts.ObjCConstantStringClass.empty()) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000465 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}