blob: 488eeac32d9984b35e177e8039867d866e277c80 [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.
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000124
125 if (Opts.AsmVerbose)
126 Res.push_back("-masm-verbose");
127 if (!Opts.CodeModel.empty()) {
128 Res.push_back("-mcode-model");
129 Res.push_back(Opts.CodeModel);
130 }
131 if (!Opts.DebugPass.empty()) {
132 Res.push_back("-mdebug-pass");
133 Res.push_back(Opts.DebugPass);
134 }
135 if (Opts.DisableFPElim)
136 Res.push_back("-mdisable-fp-elim");
137 if (!Opts.LimitFloatPrecision.empty()) {
138 Res.push_back("-mlimit-float-precision");
139 Res.push_back(Opts.LimitFloatPrecision);
140 }
141 if (Opts.NoZeroInitializedInBSS)
142 Res.push_back("-mno-zero-initialized-bss");
143 if (Opts.UnwindTables)
144 Res.push_back("-munwind-tables");
145 if (Opts.RelocationModel != "pic") {
146 Res.push_back("-mrelocation-model");
147 Res.push_back(Opts.RelocationModel);
148 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000149}
150
151static void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts,
152 std::vector<std::string> &Res) {
153 if (Opts.IncludeSystemHeaders)
154 Res.push_back("-sys-header-deps");
155 if (Opts.UsePhonyTargets)
156 Res.push_back("-MP");
157 if (!Opts.OutputFile.empty()) {
158 Res.push_back("-dependency-file");
159 Res.push_back(Opts.OutputFile);
160 }
161 for (unsigned i = 0, e = Opts.Targets.size(); i != e; ++i) {
162 Res.push_back("-MT");
163 Res.push_back(Opts.Targets[i]);
164 }
165}
166
167static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
168 std::vector<std::string> &Res) {
169 if (Opts.IgnoreWarnings)
170 Res.push_back("-w");
171 if (Opts.NoRewriteMacros)
172 Res.push_back("-Wno-rewrite-macros");
173 if (Opts.Pedantic)
174 Res.push_back("-pedantic");
175 if (Opts.PedanticErrors)
176 Res.push_back("-pedantic-errors");
177 if (!Opts.ShowColumn)
178 Res.push_back("-fno-show-column");
179 if (!Opts.ShowLocation)
180 Res.push_back("-fno-show-source-location");
181 if (!Opts.ShowCarets)
182 Res.push_back("-fno-caret-diagnostics");
183 if (!Opts.ShowFixits)
184 Res.push_back("-fno-diagnostics-fixit-info");
185 if (Opts.ShowSourceRanges)
186 Res.push_back("-fdiagnostics-print-source-range-info");
187 if (Opts.ShowColors)
188 Res.push_back("-fcolor-diagnostics");
189 if (Opts.VerifyDiagnostics)
190 Res.push_back("-verify");
191 if (Opts.ShowOptionNames)
192 Res.push_back("-fdiagnostics-show-option");
193 if (Opts.MessageLength) {
194 Res.push_back("-fmessage-length");
195 Res.push_back(llvm::utostr(Opts.MessageLength));
196 }
197 if (!Opts.DumpBuildInformation.empty()) {
198 Res.push_back("-dump-build-information");
199 Res.push_back(Opts.DumpBuildInformation);
200 }
201 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i)
202 Res.push_back("-W" + Opts.Warnings[i]);
203}
204
205static const char *getInputKindName(FrontendOptions::InputKind Kind) {
206 switch (Kind) {
Daniel Dunbar1b096952009-11-29 02:38:47 +0000207 case FrontendOptions::IK_None: break;
208 case FrontendOptions::IK_AST: return "ast";
209 case FrontendOptions::IK_Asm: return "assembler-with-cpp";
210 case FrontendOptions::IK_C: return "c";
211 case FrontendOptions::IK_CXX: return "c++";
212 case FrontendOptions::IK_ObjC: return "objective-c";
213 case FrontendOptions::IK_ObjCXX: return "objective-c++";
214 case FrontendOptions::IK_OpenCL: return "cl";
215 case FrontendOptions::IK_PreprocessedC: return "cpp-output";
216 case FrontendOptions::IK_PreprocessedCXX: return "c++-cpp-output";
217 case FrontendOptions::IK_PreprocessedObjC: return "objective-c-cpp-output";
218 case FrontendOptions::IK_PreprocessedObjCXX:return "objective-c++-cpp-output";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000219 }
220
221 llvm::llvm_unreachable("Unexpected language kind!");
222 return 0;
223}
224
225static const char *getActionName(frontend::ActionKind Kind) {
226 switch (Kind) {
227 case frontend::PluginAction:
228 case frontend::InheritanceView:
229 llvm::llvm_unreachable("Invalid kind!");
230
231 case frontend::ASTDump: return "-ast-dump";
232 case frontend::ASTPrint: return "-ast-print";
233 case frontend::ASTPrintXML: return "-ast-print-xml";
234 case frontend::ASTView: return "-ast-view";
235 case frontend::DumpRawTokens: return "-dump-raw-tokens";
236 case frontend::DumpRecordLayouts: return "-dump-record-layouts";
237 case frontend::DumpTokens: return "-dump-tokens";
238 case frontend::EmitAssembly: return "-S";
239 case frontend::EmitBC: return "-emit-llvm-bc";
240 case frontend::EmitHTML: return "-emit-html";
241 case frontend::EmitLLVM: return "-emit-llvm";
242 case frontend::EmitLLVMOnly: return "-emit-llvm-only";
243 case frontend::FixIt: return "-fixit";
244 case frontend::GeneratePCH: return "-emit-pch";
245 case frontend::GeneratePTH: return "-emit-pth";
246 case frontend::ParseNoop: return "-parse-noop";
247 case frontend::ParsePrintCallbacks: return "-parse-print-callbacks";
248 case frontend::ParseSyntaxOnly: return "-fsyntax-only";
249 case frontend::PrintDeclContext: return "-print-decl-contexts";
250 case frontend::PrintPreprocessedInput: return "-E";
251 case frontend::RewriteBlocks: return "-rewrite-blocks";
252 case frontend::RewriteMacros: return "-rewrite-macros";
253 case frontend::RewriteObjC: return "-rewrite-objc";
254 case frontend::RewriteTest: return "-rewrite-test";
255 case frontend::RunAnalysis: return "-analyze";
256 case frontend::RunPreprocessorOnly: return "-Eonly";
257 }
258
259 llvm::llvm_unreachable("Unexpected language kind!");
260 return 0;
261}
262
263static void FrontendOptsToArgs(const FrontendOptions &Opts,
264 std::vector<std::string> &Res) {
265 if (!Opts.DebugCodeCompletionPrinter)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000266 Res.push_back("-no-code-completion-debug-printer");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000267 if (Opts.DisableFree)
268 Res.push_back("-disable-free");
269 if (Opts.EmptyInputOnly)
270 Res.push_back("-empty-input-only");
271 if (Opts.RelocatablePCH)
272 Res.push_back("-relocatable-pch");
273 if (Opts.ShowMacrosInCodeCompletion)
274 Res.push_back("-code-completion-macros");
275 if (Opts.ShowStats)
Daniel Dunbareb01ac82009-11-25 10:14:52 +0000276 Res.push_back("-print-stats");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000277 if (Opts.ShowTimers)
278 Res.push_back("-ftime-report");
279
280 bool NeedLang = false;
281 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i)
282 if (FrontendOptions::getInputKindForExtension(Opts.Inputs[i].second) !=
283 Opts.Inputs[i].first)
284 NeedLang = true;
285 if (NeedLang) {
286 Res.push_back("-x");
287 Res.push_back(getInputKindName(Opts.Inputs[0].first));
288 }
289 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i) {
290 assert((!NeedLang || Opts.Inputs[i].first == Opts.Inputs[0].first) &&
291 "Unable to represent this input vector!");
292 Res.push_back(Opts.Inputs[i].second);
293 }
294
295 if (!Opts.OutputFile.empty()) {
296 Res.push_back("-o");
297 Res.push_back(Opts.OutputFile);
298 }
299 if (!Opts.ViewClassInheritance.empty()) {
300 Res.push_back("-cxx-inheritance-view");
301 Res.push_back(Opts.ViewClassInheritance);
302 }
303 for (unsigned i = 0, e = Opts.FixItLocations.size(); i != e; ++i) {
304 Res.push_back("-fixit-at");
305 Res.push_back(Opts.FixItLocations[i].FileName + ":" +
306 llvm::utostr(Opts.FixItLocations[i].Line) + ":" +
307 llvm::utostr(Opts.FixItLocations[i].Column));
308 }
309 if (!Opts.CodeCompletionAt.FileName.empty()) {
310 Res.push_back("-code-completion-at");
311 Res.push_back(Opts.CodeCompletionAt.FileName + ":" +
312 llvm::utostr(Opts.CodeCompletionAt.Line) + ":" +
313 llvm::utostr(Opts.CodeCompletionAt.Column));
314 }
315 if (Opts.ProgramAction != frontend::InheritanceView &&
316 Opts.ProgramAction != frontend::PluginAction)
317 Res.push_back(getActionName(Opts.ProgramAction));
318 if (!Opts.ActionName.empty()) {
319 Res.push_back("-plugin");
320 Res.push_back(Opts.ActionName);
321 }
322}
323
324static void HeaderSearchOptsToArgs(const HeaderSearchOptions &Opts,
325 std::vector<std::string> &Res) {
Daniel Dunbarc59dc922009-11-19 05:32:21 +0000326 if (Opts.Sysroot != "/") {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000327 Res.push_back("-isysroot");
328 Res.push_back(Opts.Sysroot);
329 }
330
331 /// User specified include entries.
332 for (unsigned i = 0, e = Opts.UserEntries.size(); i != e; ++i) {
333 const HeaderSearchOptions::Entry &E = Opts.UserEntries[i];
Daniel Dunbard8c78062009-11-26 02:13:54 +0000334 if (E.IsFramework && (E.Group != frontend::Angled || !E.IsUserSupplied))
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000335 llvm::llvm_report_error("Invalid option set!");
336 if (E.IsUserSupplied) {
337 if (E.Group == frontend::After) {
338 Res.push_back("-idirafter");
339 } else if (E.Group == frontend::Quoted) {
Daniel Dunbard8c78062009-11-26 02:13:54 +0000340 Res.push_back("-iquote");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000341 } else if (E.Group == frontend::System) {
342 Res.push_back("-isystem");
343 } else {
344 assert(E.Group == frontend::Angled && "Invalid group!");
345 Res.push_back(E.IsFramework ? "-F" : "-I");
346 }
347 } else {
348 if (E.Group != frontend::Angled && E.Group != frontend::System)
349 llvm::llvm_report_error("Invalid option set!");
350 Res.push_back(E.Group == frontend::Angled ? "-iwithprefixbefore" :
351 "-iwithprefix");
352 }
353 Res.push_back(E.Path);
354 }
355
356 if (!Opts.EnvIncPath.empty()) {
357 // FIXME: Provide an option for this, and move env detection to driver.
358 llvm::llvm_report_error("Not yet implemented!");
359 }
360 if (!Opts.CEnvIncPath.empty()) {
361 // FIXME: Provide an option for this, and move env detection to driver.
362 llvm::llvm_report_error("Not yet implemented!");
363 }
364 if (!Opts.ObjCEnvIncPath.empty()) {
365 // FIXME: Provide an option for this, and move env detection to driver.
366 llvm::llvm_report_error("Not yet implemented!");
367 }
368 if (!Opts.CXXEnvIncPath.empty()) {
369 // FIXME: Provide an option for this, and move env detection to driver.
370 llvm::llvm_report_error("Not yet implemented!");
371 }
372 if (!Opts.ObjCXXEnvIncPath.empty()) {
373 // FIXME: Provide an option for this, and move env detection to driver.
374 llvm::llvm_report_error("Not yet implemented!");
375 }
376 if (!Opts.BuiltinIncludePath.empty()) {
377 // FIXME: Provide an option for this, and move to driver.
378 }
379 if (!Opts.UseStandardIncludes)
380 Res.push_back("-nostdinc");
381 if (Opts.Verbose)
382 Res.push_back("-v");
383}
384
385static void LangOptsToArgs(const LangOptions &Opts,
386 std::vector<std::string> &Res) {
387 LangOptions DefaultLangOpts;
388
389 // FIXME: Need to set -std to get all the implicit options.
390
391 // FIXME: We want to only pass options relative to the defaults, which
392 // requires constructing a target. :(
393 //
394 // It would be better to push the all target specific choices into the driver,
395 // so that everything below that was more uniform.
396
397 if (Opts.Trigraphs)
398 Res.push_back("-trigraphs");
399 // Implicit based on the input kind:
400 // AsmPreprocessor, CPlusPlus, ObjC1, ObjC2, OpenCL
401 // Implicit based on the input language standard:
402 // BCPLComment, C99, CPlusPlus0x, Digraphs, GNUInline, ImplicitInt, GNUMode
403 if (Opts.DollarIdents)
404 Res.push_back("-fdollars-in-identifiers");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000405 if (Opts.Microsoft)
406 Res.push_back("-fms-extensions=1");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000407 if (Opts.ObjCNonFragileABI)
408 Res.push_back("-fobjc-nonfragile-abi");
409 // NoInline is implicit.
410 if (!Opts.CXXOperatorNames)
411 Res.push_back("-fno-operator-names");
412 if (Opts.PascalStrings)
413 Res.push_back("-fpascal-strings");
414 if (Opts.WritableStrings)
415 Res.push_back("-fwritable-strings");
416 if (!Opts.LaxVectorConversions)
417 Res.push_back("-fno-lax-vector-conversions");
418 if (Opts.AltiVec)
419 Res.push_back("-faltivec");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000420 if (Opts.Exceptions)
421 Res.push_back("-fexceptions");
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000422 if (!Opts.Rtti)
423 Res.push_back("-fno-rtti");
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +0000424 if (!Opts.NeXTRuntime)
425 Res.push_back("-fgnu-runtime");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000426 if (Opts.Freestanding)
427 Res.push_back("-ffreestanding");
428 if (Opts.NoBuiltin)
429 Res.push_back("-fno-builtin");
430 if (Opts.ThreadsafeStatics)
431 llvm::llvm_report_error("FIXME: Not yet implemented!");
432 if (Opts.POSIXThreads)
433 Res.push_back("-pthread");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000434 if (Opts.Blocks)
Daniel Dunbare50c1672009-11-29 05:52:21 +0000435 Res.push_back("-fblocks");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000436 if (Opts.EmitAllDecls)
437 Res.push_back("-femit-all-decls");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000438 if (!Opts.MathErrno)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000439 Res.push_back("-fno-math-errno");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000440 if (Opts.OverflowChecking)
441 Res.push_back("-ftrapv");
442 if (Opts.HeinousExtensions)
443 Res.push_back("-fheinous-gnu-extensions");
444 // Optimize is implicit.
445 // OptimizeSize is implicit.
446 if (Opts.Static)
447 Res.push_back("-static-define");
448 if (Opts.PICLevel) {
449 Res.push_back("-pic-level");
450 Res.push_back(llvm::utostr(Opts.PICLevel));
451 }
452 if (Opts.ObjCGCBitmapPrint)
453 Res.push_back("-print-ivar-layout");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000454 // FIXME: Don't forget to update when the default changes!
455 if (Opts.AccessControl)
456 Res.push_back("-faccess-control");
457 if (!Opts.CharIsSigned)
Daniel Dunbare50c1672009-11-29 05:52:21 +0000458 Res.push_back("-fno-signed-char");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000459 if (Opts.ShortWChar)
460 Res.push_back("-fshort-wchar");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000461 if (!Opts.ElideConstructors)
462 Res.push_back("-fno-elide-constructors");
463 if (Opts.getGCMode() != LangOptions::NonGC) {
464 if (Opts.getGCMode() == LangOptions::HybridGC) {
465 Res.push_back("-fobjc-gc");
466 } else {
467 assert(Opts.getGCMode() == LangOptions::GCOnly && "Invalid GC mode!");
468 Res.push_back("-fobjc-gc-only");
469 }
470 }
471 if (Opts.getVisibilityMode() != LangOptions::Default) {
472 Res.push_back("-fvisibility");
473 if (Opts.getVisibilityMode() == LangOptions::Hidden) {
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000474 Res.push_back("hidden");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000475 } else {
476 assert(Opts.getVisibilityMode() == LangOptions::Protected &&
477 "Invalid visibility!");
478 Res.push_back("protected");
479 }
480 }
481 if (Opts.getStackProtectorMode() != 0) {
482 Res.push_back("-stack-protector");
483 Res.push_back(llvm::utostr(Opts.getStackProtectorMode()));
484 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000485 if (Opts.InstantiationDepth != DefaultLangOpts.InstantiationDepth) {
486 Res.push_back("-ftemplate-depth");
487 Res.push_back(llvm::utostr(Opts.InstantiationDepth));
488 }
Daniel Dunbar1b096952009-11-29 02:38:47 +0000489 if (!Opts.ObjCConstantStringClass.empty()) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000490 Res.push_back("-fconstant-string-class");
491 Res.push_back(Opts.ObjCConstantStringClass);
492 }
493}
494
495static void PreprocessorOptsToArgs(const PreprocessorOptions &Opts,
496 std::vector<std::string> &Res) {
497 for (unsigned i = 0, e = Opts.Macros.size(); i != e; ++i)
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000498 Res.push_back(std::string(Opts.Macros[i].second ? "-U" : "-D") +
499 Opts.Macros[i].first);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000500 for (unsigned i = 0, e = Opts.Includes.size(); i != e; ++i) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000501 // FIXME: We need to avoid reincluding the implicit PCH and PTH includes.
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000502 Res.push_back("-include");
503 Res.push_back(Opts.Includes[i]);
504 }
505 for (unsigned i = 0, e = Opts.MacroIncludes.size(); i != e; ++i) {
506 Res.push_back("-imacros");
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000507 Res.push_back(Opts.MacroIncludes[i]);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000508 }
509 if (!Opts.UsePredefines)
510 Res.push_back("-undef");
511 if (!Opts.ImplicitPCHInclude.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000512 Res.push_back("-include-pch");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000513 Res.push_back(Opts.ImplicitPCHInclude);
514 }
515 if (!Opts.ImplicitPTHInclude.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000516 Res.push_back("-include-pth");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000517 Res.push_back(Opts.ImplicitPTHInclude);
518 }
519 if (!Opts.TokenCache.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000520 if (Opts.ImplicitPTHInclude.empty()) {
521 Res.push_back("-token-cache");
522 Res.push_back(Opts.TokenCache);
523 } else
524 assert(Opts.ImplicitPTHInclude == Opts.TokenCache &&
525 "Unsupported option combination!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000526 }
527}
528
529static void PreprocessorOutputOptsToArgs(const PreprocessorOutputOptions &Opts,
530 std::vector<std::string> &Res) {
531 if (!Opts.ShowCPP && !Opts.ShowMacros)
532 llvm::llvm_report_error("Invalid option combination!");
533
534 if (Opts.ShowCPP && Opts.ShowMacros)
535 Res.push_back("-dD");
536 else if (!Opts.ShowCPP && Opts.ShowMacros)
537 Res.push_back("-dM");
538
539 if (!Opts.ShowLineMarkers)
540 Res.push_back("-P");
541 if (Opts.ShowComments)
542 Res.push_back("-C");
543 if (Opts.ShowMacroComments)
544 Res.push_back("-CC");
545}
546
547static void TargetOptsToArgs(const TargetOptions &Opts,
548 std::vector<std::string> &Res) {
549 Res.push_back("-triple");
550 Res.push_back(Opts.Triple);
551 if (!Opts.CPU.empty()) {
Daniel Dunbar21affc02009-11-23 23:41:17 +0000552 Res.push_back("-mcpu");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000553 Res.push_back(Opts.CPU);
554 }
555 if (!Opts.ABI.empty()) {
556 Res.push_back("-target-abi");
557 Res.push_back(Opts.ABI);
558 }
559 for (unsigned i = 0, e = Opts.Features.size(); i != e; ++i) {
560 Res.push_back("-target-feature");
561 Res.push_back(Opts.Features[i]);
562 }
563}
564
565void CompilerInvocation::toArgs(std::vector<std::string> &Res) {
566 AnalyzerOptsToArgs(getAnalyzerOpts(), Res);
567 CodeGenOptsToArgs(getCodeGenOpts(), Res);
568 DependencyOutputOptsToArgs(getDependencyOutputOpts(), Res);
569 DiagnosticOptsToArgs(getDiagnosticOpts(), Res);
570 FrontendOptsToArgs(getFrontendOpts(), Res);
571 HeaderSearchOptsToArgs(getHeaderSearchOpts(), Res);
572 LangOptsToArgs(getLangOpts(), Res);
573 PreprocessorOptsToArgs(getPreprocessorOpts(), Res);
574 PreprocessorOutputOptsToArgs(getPreprocessorOutputOpts(), Res);
575 TargetOptsToArgs(getTargetOpts(), Res);
576}