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