CompilerInvocation.cpp revision 199482
1139825Simp//===--- CompilerInvocation.cpp -------------------------------------------===//
21541Srgrimes//
31541Srgrimes//                     The LLVM Compiler Infrastructure
41541Srgrimes//
51541Srgrimes// This file is distributed under the University of Illinois Open Source
61541Srgrimes// License. See LICENSE.TXT for details.
71541Srgrimes//
81541Srgrimes//===----------------------------------------------------------------------===//
91541Srgrimes
101541Srgrimes#include "clang/Frontend/CompilerInvocation.h"
111541Srgrimes#include "llvm/ADT/StringExtras.h"
121541Srgrimes#include "llvm/Support/ErrorHandling.h"
131541Srgrimesusing namespace clang;
141541Srgrimes
151541Srgrimesvoid CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
161541Srgrimes                           const llvm::SmallVectorImpl<llvm::StringRef> &Args) {
171541Srgrimes  llvm::llvm_report_error("FIXME: Not yet implemented!");
181541Srgrimes}
191541Srgrimes
201541Srgrimesstatic const char *getAnalysisName(Analyses Kind) {
211541Srgrimes  switch (Kind) {
221541Srgrimes  default:
231541Srgrimes    llvm::llvm_unreachable("Unknown analysis store!");
241541Srgrimes#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
251541Srgrimes  case NAME: return CMDFLAG;
261541Srgrimes#include "clang/Frontend/Analyses.def"
271541Srgrimes  }
281541Srgrimes}
291541Srgrimes
301541Srgrimesstatic const char *getAnalysisStoreName(AnalysisStores Kind) {
311541Srgrimes  switch (Kind) {
321541Srgrimes  default:
331541Srgrimes    llvm::llvm_unreachable("Unknown analysis store!");
3422521Sdyson#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) \
351541Srgrimes  case NAME##Model: return CMDFLAG;
361541Srgrimes#include "clang/Frontend/Analyses.def"
37116192Sobrien  }
38116192Sobrien}
39116192Sobrien
4013260Swollmanstatic const char *getAnalysisConstraintName(AnalysisConstraints Kind) {
4199101Siedowse  switch (Kind) {
4213260Swollman  default:
431541Srgrimes    llvm::llvm_unreachable("Unknown analysis constraints!");
4476166Smarkm#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) \
4541059Speter  case NAME##Model: return CMDFLAG;
4676166Smarkm#include "clang/Frontend/Analyses.def"
4776166Smarkm  }
4876166Smarkm}
4965557Sjasone
5076166Smarkmstatic const char *getAnalysisDiagClientName(AnalysisDiagClients Kind) {
511541Srgrimes  switch (Kind) {
521541Srgrimes  default:
5359241Srwatson    llvm::llvm_unreachable("Unknown analysis client!");
541541Srgrimes#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE) \
551541Srgrimes  case PD_##NAME: return CMDFLAG;
561541Srgrimes#include "clang/Frontend/Analyses.def"
571541Srgrimes  }
5899101Siedowse}
5999101Siedowse
6099101Siedowsestatic void AnalyzerOptsToArgs(const AnalyzerOptions &Opts,
6199101Siedowse                               std::vector<std::string> &Res) {
621541Srgrimes  for (unsigned i = 0, e = Opts.AnalysisList.size(); i != e; ++i)
63151897Srwatson    Res.push_back(getAnalysisName(Opts.AnalysisList[i]));
641541Srgrimes  if (Opts.AnalysisStoreOpt != BasicStoreModel) {
651541Srgrimes    Res.push_back("-analyzer-store");
661541Srgrimes    Res.push_back(getAnalysisStoreName(Opts.AnalysisStoreOpt));
671541Srgrimes  }
681541Srgrimes  if (Opts.AnalysisConstraintsOpt != RangeConstraintsModel) {
69191990Sattilio    Res.push_back("-analyzer-constraints");
701541Srgrimes    Res.push_back(getAnalysisConstraintName(Opts.AnalysisConstraintsOpt));
71144056Sjeff  }
721541Srgrimes  if (Opts.AnalysisDiagOpt != PD_HTML) {
731541Srgrimes    Res.push_back("-analyzer-output");
741541Srgrimes    Res.push_back(getAnalysisDiagClientName(Opts.AnalysisDiagOpt));
751541Srgrimes  }
761541Srgrimes  if (!Opts.AnalyzeSpecificFunction.empty()) {
77144056Sjeff    Res.push_back("-analyze-function");
783427Sphk    Res.push_back(Opts.AnalyzeSpecificFunction);
791541Srgrimes  }
801541Srgrimes  if (Opts.AnalyzeAll)
811541Srgrimes    Res.push_back("-analyzer-opt-analyze-headers");
821541Srgrimes  if (Opts.AnalyzerDisplayProgress)
831541Srgrimes    Res.push_back("-analyzer-display-progress");
841541Srgrimes  if (Opts.EagerlyAssume)
851541Srgrimes    Res.push_back("-analyzer-eagerly-assume");
861541Srgrimes  if (Opts.PurgeDead)
871541Srgrimes    Res.push_back("-analyzer-purge-dead");
88191990Sattilio  if (Opts.TrimGraph)
891541Srgrimes    Res.push_back("-trim-egraph");
901541Srgrimes  if (Opts.VisualizeEGDot)
91166381Smpp    Res.push_back("-analyzer-viz-egraph-graphviz");
92153400Sdes  if (Opts.VisualizeEGDot)
931541Srgrimes    Res.push_back("-analyzer-viz-egraph-ubigraph");
941541Srgrimes  if (Opts.EnableExperimentalChecks)
95278150Skib    Res.push_back("-analyzer-experimental-checks");
96278150Skib  if (Opts.EnableExperimentalInternalChecks)
97278150Skib    Res.push_back("-analyzer-experimental-internal-checls");
981541Srgrimes}
991541Srgrimes
100191990Sattiliostatic void CodeGenOptsToArgs(const CodeGenOptions &Opts,
1013427Sphk                              std::vector<std::string> &Res) {
1023427Sphk  if (Opts.DebugInfo)
103191990Sattilio    Res.push_back("-g");
1041541Srgrimes  if (Opts.DisableLLVMOpts)
1056992Sdg    Res.push_back("-disable-llvm-optzns");
106166381Smpp  if (Opts.DisableRedZone)
107166381Smpp    Res.push_back("-disable-red-zone");
108166381Smpp  if (!Opts.MergeAllConstants)
109166381Smpp    Res.push_back("-fno-merge-all-constants");
110166381Smpp  // NoCommon is only derived.
111166381Smpp  if (Opts.NoImplicitFloat)
112166381Smpp    Res.push_back("-no-implicit-float");
113166381Smpp  if (Opts.OptimizeSize) {
114166381Smpp    assert(Opts.OptimizationLevel == 2 && "Invalid options!");
115166381Smpp    Res.push_back("-Os");
116166381Smpp  } else if (Opts.OptimizationLevel == 0)
117166381Smpp    Res.push_back("-O" + Opts.OptimizationLevel);
118278150Skib  // SimplifyLibCalls is only derived.
119278150Skib  // TimePasses is only derived.
120166381Smpp  // UnitAtATime is unused.
121166381Smpp  // UnrollLoops is only derived.
122166381Smpp  // VerifyModule is only derived.
123278150Skib  // Inlining is only derived.
124278150Skib}
125278150Skib
1261541Srgrimesstatic void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts,
127278150Skib                                       std::vector<std::string> &Res) {
128116384Srwatson  if (Opts.IncludeSystemHeaders)
1291541Srgrimes    Res.push_back("-sys-header-deps");
1301541Srgrimes  if (Opts.UsePhonyTargets)
13183366Sjulian    Res.push_back("-MP");
13222521Sdyson  if (!Opts.OutputFile.empty()) {
1331541Srgrimes    Res.push_back("-dependency-file");
1341541Srgrimes    Res.push_back(Opts.OutputFile);
13583366Sjulian  }
13622521Sdyson  for (unsigned i = 0, e = Opts.Targets.size(); i != e; ++i) {
1371541Srgrimes    Res.push_back("-MT");
138207736Smckusick    Res.push_back(Opts.Targets[i]);
139207736Smckusick  }
140207736Smckusick}
141207736Smckusick
142207736Smckusickstatic void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
143207736Smckusick                                 std::vector<std::string> &Res) {
144207736Smckusick  if (Opts.IgnoreWarnings)
145207736Smckusick    Res.push_back("-w");
146207736Smckusick  if (Opts.NoRewriteMacros)
147207736Smckusick    Res.push_back("-Wno-rewrite-macros");
148207736Smckusick  if (Opts.Pedantic)
149207736Smckusick    Res.push_back("-pedantic");
1501541Srgrimes  if (Opts.PedanticErrors)
151166381Smpp    Res.push_back("-pedantic-errors");
15222521Sdyson  if (!Opts.ShowColumn)
1531541Srgrimes    Res.push_back("-fno-show-column");
1541541Srgrimes  if (!Opts.ShowLocation)
155166381Smpp    Res.push_back("-fno-show-source-location");
15622521Sdyson  if (!Opts.ShowCarets)
1571541Srgrimes    Res.push_back("-fno-caret-diagnostics");
1581541Srgrimes  if (!Opts.ShowFixits)
159166381Smpp    Res.push_back("-fno-diagnostics-fixit-info");
16022521Sdyson  if (Opts.ShowSourceRanges)
1611541Srgrimes    Res.push_back("-fdiagnostics-print-source-range-info");
162207736Smckusick  if (Opts.ShowColors)
163207736Smckusick    Res.push_back("-fcolor-diagnostics");
164207736Smckusick  if (Opts.VerifyDiagnostics)
165207736Smckusick    Res.push_back("-verify");
1661541Srgrimes  if (Opts.ShowOptionNames)
1671541Srgrimes    Res.push_back("-fdiagnostics-show-option");
16822521Sdyson  if (Opts.MessageLength) {
1691541Srgrimes    Res.push_back("-fmessage-length");
1701541Srgrimes    Res.push_back(llvm::utostr(Opts.MessageLength));
17122521Sdyson  }
17222521Sdyson  if (!Opts.DumpBuildInformation.empty()) {
1731541Srgrimes    Res.push_back("-dump-build-information");
17422521Sdyson    Res.push_back(Opts.DumpBuildInformation);
1751541Srgrimes  }
1761541Srgrimes  for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i)
1771541Srgrimes    Res.push_back("-W" + Opts.Warnings[i]);
1781541Srgrimes}
17922521Sdyson
18022521Sdysonstatic const char *getInputKindName(FrontendOptions::InputKind Kind) {
18122521Sdyson  switch (Kind) {
18222521Sdyson  case FrontendOptions::IK_None: break;
18322521Sdyson  case FrontendOptions::IK_AST: return "ast";
18422521Sdyson  case FrontendOptions::IK_Asm: return "assembler-with-cpp";
18522521Sdyson  case FrontendOptions::IK_C: return "c";
18622521Sdyson  case FrontendOptions::IK_CXX: return "c++";
18722521Sdyson  case FrontendOptions::IK_ObjC: return "objective-c";
18822521Sdyson  case FrontendOptions::IK_ObjCXX: return "objective-c++";
18999101Siedowse  case FrontendOptions::IK_OpenCL: return "cl";
19099101Siedowse  case FrontendOptions::IK_PreprocessedC: return "cpp-output";
19199101Siedowse  case FrontendOptions::IK_PreprocessedCXX: return "c++-cpp-output";
19222521Sdyson  case FrontendOptions::IK_PreprocessedObjC: return "objective-c-cpp-output";
19322521Sdyson  case FrontendOptions::IK_PreprocessedObjCXX: return "objective-c++-cpp-output";
19422521Sdyson  }
19522521Sdyson
19699101Siedowse  llvm::llvm_unreachable("Unexpected language kind!");
19799101Siedowse  return 0;
19899101Siedowse}
19999101Siedowse
20099101Siedowsestatic const char *getActionName(frontend::ActionKind Kind) {
20199101Siedowse  switch (Kind) {
20299101Siedowse  case frontend::PluginAction:
20399101Siedowse  case frontend::InheritanceView:
20499101Siedowse    llvm::llvm_unreachable("Invalid kind!");
20599101Siedowse
20699101Siedowse  case frontend::ASTDump:                return "-ast-dump";
20799101Siedowse  case frontend::ASTPrint:               return "-ast-print";
20899101Siedowse  case frontend::ASTPrintXML:            return "-ast-print-xml";
20999101Siedowse  case frontend::ASTView:                return "-ast-view";
21099101Siedowse  case frontend::DumpRawTokens:          return "-dump-raw-tokens";
21199101Siedowse  case frontend::DumpRecordLayouts:      return "-dump-record-layouts";
21299101Siedowse  case frontend::DumpTokens:             return "-dump-tokens";
2131541Srgrimes  case frontend::EmitAssembly:           return "-S";
2141541Srgrimes  case frontend::EmitBC:                 return "-emit-llvm-bc";
2151541Srgrimes  case frontend::EmitHTML:               return "-emit-html";
21651138Salfred  case frontend::EmitLLVM:               return "-emit-llvm";
2171541Srgrimes  case frontend::EmitLLVMOnly:           return "-emit-llvm-only";
2181541Srgrimes  case frontend::FixIt:                  return "-fixit";
219222167Srmacklem  case frontend::GeneratePCH:            return "-emit-pch";
22096482Sphk  case frontend::GeneratePTH:            return "-emit-pth";
2211541Srgrimes  case frontend::ParseNoop:              return "-parse-noop";
222222167Srmacklem  case frontend::ParsePrintCallbacks:    return "-parse-print-callbacks";
2231541Srgrimes  case frontend::ParseSyntaxOnly:        return "-fsyntax-only";
2241541Srgrimes  case frontend::PrintDeclContext:       return "-print-decl-contexts";
22596482Sphk  case frontend::PrintPreprocessedInput: return "-E";
2261541Srgrimes  case frontend::RewriteBlocks:          return "-rewrite-blocks";
2271541Srgrimes  case frontend::RewriteMacros:          return "-rewrite-macros";
2281541Srgrimes  case frontend::RewriteObjC:            return "-rewrite-objc";
229222196Srmacklem  case frontend::RewriteTest:            return "-rewrite-test";
2303427Sphk  case frontend::RunAnalysis:            return "-analyze";
2311541Srgrimes  case frontend::RunPreprocessorOnly:    return "-Eonly";
2321541Srgrimes  }
2331541Srgrimes
2341541Srgrimes  llvm::llvm_unreachable("Unexpected language kind!");
23596873Siedowse  return 0;
23696873Siedowse}
2371541Srgrimes
2381541Srgrimesstatic void FrontendOptsToArgs(const FrontendOptions &Opts,
2391541Srgrimes                               std::vector<std::string> &Res) {
2401541Srgrimes  if (!Opts.DebugCodeCompletionPrinter)
2411541Srgrimes    Res.push_back("-code-completion-debug-printer=0");
242140962Speadar  if (Opts.DisableFree)
24351138Salfred    Res.push_back("-disable-free");
24451138Salfred  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)
251    Res.push_back("-stats");
252  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) {
301  if (Opts.Sysroot.empty()) {
302    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];
309    if (E.IsFramework && (E.Group != frontend::Angled || E.IsUserSupplied))
310      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) {
315        Res.push_back("-iquoted");
316      } 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");
380  if (Opts.Microsoft)
381    Res.push_back("-fms-extensions=1");
382  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");
395  Res.push_back("-fexceptions");
396  Res.push_back(Opts.Exceptions ? "1" : "0");
397  Res.push_back("-frtti");
398  Res.push_back(Opts.Rtti ? "1" : "0");
399  if (!Opts.NeXTRuntime)
400    Res.push_back("-fgnu-runtime");
401  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");
409  if (Opts.Blocks)
410    Res.push_back("-fblocks=1");
411  if (Opts.EmitAllDecls)
412    Res.push_back("-femit-all-decls");
413  if (!Opts.MathErrno)
414    Res.push_back("-fmath-errno=0");
415  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");
429  Res.push_back("-faccess-control");
430  Res.push_back(Opts.AccessControl ? "1" : "0");
431  Res.push_back("-fsigned-char");
432  Res.push_back(Opts.CharIsSigned ? "1" : "0");
433  Res.push_back("-fshort-wchar");
434  Res.push_back(Opts.ShortWChar ? "1" : "0");
435  if (!Opts.ElideConstructors)
436    Res.push_back("-fno-elide-constructors");
437  if (Opts.getGCMode() != LangOptions::NonGC) {
438    if (Opts.getGCMode() == LangOptions::HybridGC) {
439      Res.push_back("-fobjc-gc");
440    } else {
441      assert(Opts.getGCMode() == LangOptions::GCOnly && "Invalid GC mode!");
442      Res.push_back("-fobjc-gc-only");
443    }
444  }
445  if (Opts.getVisibilityMode() != LangOptions::Default) {
446    Res.push_back("-fvisibility");
447    if (Opts.getVisibilityMode() == LangOptions::Hidden) {
448      Res.push_back("default");
449    } else {
450      assert(Opts.getVisibilityMode() == LangOptions::Protected &&
451             "Invalid visibility!");
452      Res.push_back("protected");
453    }
454  }
455  if (Opts.getStackProtectorMode() != 0) {
456    Res.push_back("-stack-protector");
457    Res.push_back(llvm::utostr(Opts.getStackProtectorMode()));
458  }
459  if (Opts.getMainFileName()) {
460    Res.push_back("-main-file-name");
461    Res.push_back(Opts.getMainFileName());
462  }
463  if (Opts.InstantiationDepth != DefaultLangOpts.InstantiationDepth) {
464    Res.push_back("-ftemplate-depth");
465    Res.push_back(llvm::utostr(Opts.InstantiationDepth));
466  }
467  if (Opts.ObjCConstantStringClass) {
468    Res.push_back("-fconstant-string-class");
469    Res.push_back(Opts.ObjCConstantStringClass);
470  }
471}
472
473static void PreprocessorOptsToArgs(const PreprocessorOptions &Opts,
474                                   std::vector<std::string> &Res) {
475  for (unsigned i = 0, e = Opts.Macros.size(); i != e; ++i)
476    Res.push_back((Opts.Macros[i].second ? "-U" : "-D") + Opts.Macros[i].first);
477  for (unsigned i = 0, e = Opts.Includes.size(); i != e; ++i) {
478    Res.push_back("-include");
479    Res.push_back(Opts.Includes[i]);
480  }
481  for (unsigned i = 0, e = Opts.MacroIncludes.size(); i != e; ++i) {
482    Res.push_back("-imacros");
483    Res.push_back(Opts.Includes[i]);
484  }
485  if (!Opts.UsePredefines)
486    Res.push_back("-undef");
487  if (!Opts.ImplicitPCHInclude.empty()) {
488    Res.push_back("-implicit-pch-include");
489    Res.push_back(Opts.ImplicitPCHInclude);
490  }
491  if (!Opts.ImplicitPTHInclude.empty()) {
492    Res.push_back("-implicit-pth-include");
493    Res.push_back(Opts.ImplicitPTHInclude);
494  }
495  if (!Opts.TokenCache.empty()) {
496    Res.push_back("-token-cache");
497    Res.push_back(Opts.TokenCache);
498  }
499}
500
501static void PreprocessorOutputOptsToArgs(const PreprocessorOutputOptions &Opts,
502                                         std::vector<std::string> &Res) {
503  if (!Opts.ShowCPP && !Opts.ShowMacros)
504    llvm::llvm_report_error("Invalid option combination!");
505
506  if (Opts.ShowCPP && Opts.ShowMacros)
507    Res.push_back("-dD");
508  else if (!Opts.ShowCPP && Opts.ShowMacros)
509    Res.push_back("-dM");
510
511  if (!Opts.ShowLineMarkers)
512    Res.push_back("-P");
513  if (Opts.ShowComments)
514    Res.push_back("-C");
515  if (Opts.ShowMacroComments)
516    Res.push_back("-CC");
517}
518
519static void TargetOptsToArgs(const TargetOptions &Opts,
520                             std::vector<std::string> &Res) {
521  Res.push_back("-triple");
522  Res.push_back(Opts.Triple);
523  if (!Opts.CPU.empty()) {
524    Res.push_back("-target-cpu");
525    Res.push_back(Opts.CPU);
526  }
527  if (!Opts.ABI.empty()) {
528    Res.push_back("-target-abi");
529    Res.push_back(Opts.ABI);
530  }
531  for (unsigned i = 0, e = Opts.Features.size(); i != e; ++i) {
532    Res.push_back("-target-feature");
533    Res.push_back(Opts.Features[i]);
534  }
535}
536
537void CompilerInvocation::toArgs(std::vector<std::string> &Res) {
538  AnalyzerOptsToArgs(getAnalyzerOpts(), Res);
539  CodeGenOptsToArgs(getCodeGenOpts(), Res);
540  DependencyOutputOptsToArgs(getDependencyOutputOpts(), Res);
541  DiagnosticOptsToArgs(getDiagnosticOpts(), Res);
542  FrontendOptsToArgs(getFrontendOpts(), Res);
543  HeaderSearchOptsToArgs(getHeaderSearchOpts(), Res);
544  LangOptsToArgs(getLangOpts(), Res);
545  PreprocessorOptsToArgs(getPreprocessorOpts(), Res);
546  PreprocessorOutputOptsToArgs(getPreprocessorOutputOpts(), Res);
547  TargetOptsToArgs(getTargetOpts(), Res);
548}
549