Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 13 | using namespace clang; |
| 14 | |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 15 | static const char *getAnalysisName(Analyses Kind) { |
| 16 | switch (Kind) { |
| 17 | default: |
| 18 | llvm::llvm_unreachable("Unknown analysis store!"); |
| 19 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
| 20 | case NAME: return CMDFLAG; |
| 21 | #include "clang/Frontend/Analyses.def" |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | static 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 | |
| 35 | static 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 | |
| 45 | static 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 | |
| 55 | static 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 Dunbar | 4db166b | 2009-11-19 05:32:09 +0000 | [diff] [blame] | 81 | if (!Opts.PurgeDead) |
| 82 | Res.push_back("-analyzer-no-purge-dead"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 83 | 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) |
| 92 | Res.push_back("-analyzer-experimental-internal-checls"); |
| 93 | } |
| 94 | |
| 95 | static 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"); |
| 105 | // NoCommon is only derived. |
| 106 | if (Opts.NoImplicitFloat) |
| 107 | Res.push_back("-no-implicit-float"); |
| 108 | if (Opts.OptimizeSize) { |
| 109 | assert(Opts.OptimizationLevel == 2 && "Invalid options!"); |
| 110 | Res.push_back("-Os"); |
Daniel Dunbar | 1be3b3b | 2009-11-19 20:54:59 +0000 | [diff] [blame] | 111 | } else if (Opts.OptimizationLevel != 0) |
| 112 | Res.push_back("-O" + llvm::utostr(Opts.OptimizationLevel)); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 113 | // SimplifyLibCalls is only derived. |
| 114 | // TimePasses is only derived. |
| 115 | // UnitAtATime is unused. |
| 116 | // UnrollLoops is only derived. |
| 117 | // VerifyModule is only derived. |
| 118 | // Inlining is only derived. |
| 119 | } |
| 120 | |
| 121 | static void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts, |
| 122 | std::vector<std::string> &Res) { |
| 123 | if (Opts.IncludeSystemHeaders) |
| 124 | Res.push_back("-sys-header-deps"); |
| 125 | if (Opts.UsePhonyTargets) |
| 126 | Res.push_back("-MP"); |
| 127 | if (!Opts.OutputFile.empty()) { |
| 128 | Res.push_back("-dependency-file"); |
| 129 | Res.push_back(Opts.OutputFile); |
| 130 | } |
| 131 | for (unsigned i = 0, e = Opts.Targets.size(); i != e; ++i) { |
| 132 | Res.push_back("-MT"); |
| 133 | Res.push_back(Opts.Targets[i]); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts, |
| 138 | std::vector<std::string> &Res) { |
| 139 | if (Opts.IgnoreWarnings) |
| 140 | Res.push_back("-w"); |
| 141 | if (Opts.NoRewriteMacros) |
| 142 | Res.push_back("-Wno-rewrite-macros"); |
| 143 | if (Opts.Pedantic) |
| 144 | Res.push_back("-pedantic"); |
| 145 | if (Opts.PedanticErrors) |
| 146 | Res.push_back("-pedantic-errors"); |
| 147 | if (!Opts.ShowColumn) |
| 148 | Res.push_back("-fno-show-column"); |
| 149 | if (!Opts.ShowLocation) |
| 150 | Res.push_back("-fno-show-source-location"); |
| 151 | if (!Opts.ShowCarets) |
| 152 | Res.push_back("-fno-caret-diagnostics"); |
| 153 | if (!Opts.ShowFixits) |
| 154 | Res.push_back("-fno-diagnostics-fixit-info"); |
| 155 | if (Opts.ShowSourceRanges) |
| 156 | Res.push_back("-fdiagnostics-print-source-range-info"); |
| 157 | if (Opts.ShowColors) |
| 158 | Res.push_back("-fcolor-diagnostics"); |
| 159 | if (Opts.VerifyDiagnostics) |
| 160 | Res.push_back("-verify"); |
| 161 | if (Opts.ShowOptionNames) |
| 162 | Res.push_back("-fdiagnostics-show-option"); |
| 163 | if (Opts.MessageLength) { |
| 164 | Res.push_back("-fmessage-length"); |
| 165 | Res.push_back(llvm::utostr(Opts.MessageLength)); |
| 166 | } |
| 167 | if (!Opts.DumpBuildInformation.empty()) { |
| 168 | Res.push_back("-dump-build-information"); |
| 169 | Res.push_back(Opts.DumpBuildInformation); |
| 170 | } |
| 171 | for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) |
| 172 | Res.push_back("-W" + Opts.Warnings[i]); |
| 173 | } |
| 174 | |
| 175 | static const char *getInputKindName(FrontendOptions::InputKind Kind) { |
| 176 | switch (Kind) { |
| 177 | case FrontendOptions::IK_None: break; |
| 178 | case FrontendOptions::IK_AST: return "ast"; |
| 179 | case FrontendOptions::IK_Asm: return "assembler-with-cpp"; |
| 180 | case FrontendOptions::IK_C: return "c"; |
| 181 | case FrontendOptions::IK_CXX: return "c++"; |
| 182 | case FrontendOptions::IK_ObjC: return "objective-c"; |
| 183 | case FrontendOptions::IK_ObjCXX: return "objective-c++"; |
| 184 | case FrontendOptions::IK_OpenCL: return "cl"; |
| 185 | case FrontendOptions::IK_PreprocessedC: return "cpp-output"; |
| 186 | case FrontendOptions::IK_PreprocessedCXX: return "c++-cpp-output"; |
| 187 | case FrontendOptions::IK_PreprocessedObjC: return "objective-c-cpp-output"; |
| 188 | case FrontendOptions::IK_PreprocessedObjCXX: return "objective-c++-cpp-output"; |
| 189 | } |
| 190 | |
| 191 | llvm::llvm_unreachable("Unexpected language kind!"); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static const char *getActionName(frontend::ActionKind Kind) { |
| 196 | switch (Kind) { |
| 197 | case frontend::PluginAction: |
| 198 | case frontend::InheritanceView: |
| 199 | llvm::llvm_unreachable("Invalid kind!"); |
| 200 | |
| 201 | case frontend::ASTDump: return "-ast-dump"; |
| 202 | case frontend::ASTPrint: return "-ast-print"; |
| 203 | case frontend::ASTPrintXML: return "-ast-print-xml"; |
| 204 | case frontend::ASTView: return "-ast-view"; |
| 205 | case frontend::DumpRawTokens: return "-dump-raw-tokens"; |
| 206 | case frontend::DumpRecordLayouts: return "-dump-record-layouts"; |
| 207 | case frontend::DumpTokens: return "-dump-tokens"; |
| 208 | case frontend::EmitAssembly: return "-S"; |
| 209 | case frontend::EmitBC: return "-emit-llvm-bc"; |
| 210 | case frontend::EmitHTML: return "-emit-html"; |
| 211 | case frontend::EmitLLVM: return "-emit-llvm"; |
| 212 | case frontend::EmitLLVMOnly: return "-emit-llvm-only"; |
| 213 | case frontend::FixIt: return "-fixit"; |
| 214 | case frontend::GeneratePCH: return "-emit-pch"; |
| 215 | case frontend::GeneratePTH: return "-emit-pth"; |
| 216 | case frontend::ParseNoop: return "-parse-noop"; |
| 217 | case frontend::ParsePrintCallbacks: return "-parse-print-callbacks"; |
| 218 | case frontend::ParseSyntaxOnly: return "-fsyntax-only"; |
| 219 | case frontend::PrintDeclContext: return "-print-decl-contexts"; |
| 220 | case frontend::PrintPreprocessedInput: return "-E"; |
| 221 | case frontend::RewriteBlocks: return "-rewrite-blocks"; |
| 222 | case frontend::RewriteMacros: return "-rewrite-macros"; |
| 223 | case frontend::RewriteObjC: return "-rewrite-objc"; |
| 224 | case frontend::RewriteTest: return "-rewrite-test"; |
| 225 | case frontend::RunAnalysis: return "-analyze"; |
| 226 | case frontend::RunPreprocessorOnly: return "-Eonly"; |
| 227 | } |
| 228 | |
| 229 | llvm::llvm_unreachable("Unexpected language kind!"); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static void FrontendOptsToArgs(const FrontendOptions &Opts, |
| 234 | std::vector<std::string> &Res) { |
| 235 | if (!Opts.DebugCodeCompletionPrinter) |
Daniel Dunbar | 4db166b | 2009-11-19 05:32:09 +0000 | [diff] [blame] | 236 | Res.push_back("-no-code-completion-debug-printer"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 237 | if (Opts.DisableFree) |
| 238 | Res.push_back("-disable-free"); |
| 239 | if (Opts.EmptyInputOnly) |
| 240 | Res.push_back("-empty-input-only"); |
| 241 | if (Opts.RelocatablePCH) |
| 242 | Res.push_back("-relocatable-pch"); |
| 243 | if (Opts.ShowMacrosInCodeCompletion) |
| 244 | Res.push_back("-code-completion-macros"); |
| 245 | if (Opts.ShowStats) |
| 246 | Res.push_back("-stats"); |
| 247 | if (Opts.ShowTimers) |
| 248 | Res.push_back("-ftime-report"); |
| 249 | |
| 250 | bool NeedLang = false; |
| 251 | for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i) |
| 252 | if (FrontendOptions::getInputKindForExtension(Opts.Inputs[i].second) != |
| 253 | Opts.Inputs[i].first) |
| 254 | NeedLang = true; |
| 255 | if (NeedLang) { |
| 256 | Res.push_back("-x"); |
| 257 | Res.push_back(getInputKindName(Opts.Inputs[0].first)); |
| 258 | } |
| 259 | for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i) { |
| 260 | assert((!NeedLang || Opts.Inputs[i].first == Opts.Inputs[0].first) && |
| 261 | "Unable to represent this input vector!"); |
| 262 | Res.push_back(Opts.Inputs[i].second); |
| 263 | } |
| 264 | |
| 265 | if (!Opts.OutputFile.empty()) { |
| 266 | Res.push_back("-o"); |
| 267 | Res.push_back(Opts.OutputFile); |
| 268 | } |
| 269 | if (!Opts.ViewClassInheritance.empty()) { |
| 270 | Res.push_back("-cxx-inheritance-view"); |
| 271 | Res.push_back(Opts.ViewClassInheritance); |
| 272 | } |
| 273 | for (unsigned i = 0, e = Opts.FixItLocations.size(); i != e; ++i) { |
| 274 | Res.push_back("-fixit-at"); |
| 275 | Res.push_back(Opts.FixItLocations[i].FileName + ":" + |
| 276 | llvm::utostr(Opts.FixItLocations[i].Line) + ":" + |
| 277 | llvm::utostr(Opts.FixItLocations[i].Column)); |
| 278 | } |
| 279 | if (!Opts.CodeCompletionAt.FileName.empty()) { |
| 280 | Res.push_back("-code-completion-at"); |
| 281 | Res.push_back(Opts.CodeCompletionAt.FileName + ":" + |
| 282 | llvm::utostr(Opts.CodeCompletionAt.Line) + ":" + |
| 283 | llvm::utostr(Opts.CodeCompletionAt.Column)); |
| 284 | } |
| 285 | if (Opts.ProgramAction != frontend::InheritanceView && |
| 286 | Opts.ProgramAction != frontend::PluginAction) |
| 287 | Res.push_back(getActionName(Opts.ProgramAction)); |
| 288 | if (!Opts.ActionName.empty()) { |
| 289 | Res.push_back("-plugin"); |
| 290 | Res.push_back(Opts.ActionName); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void HeaderSearchOptsToArgs(const HeaderSearchOptions &Opts, |
| 295 | std::vector<std::string> &Res) { |
Daniel Dunbar | c59dc92 | 2009-11-19 05:32:21 +0000 | [diff] [blame] | 296 | if (Opts.Sysroot != "/") { |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 297 | Res.push_back("-isysroot"); |
| 298 | Res.push_back(Opts.Sysroot); |
| 299 | } |
| 300 | |
| 301 | /// User specified include entries. |
| 302 | for (unsigned i = 0, e = Opts.UserEntries.size(); i != e; ++i) { |
| 303 | const HeaderSearchOptions::Entry &E = Opts.UserEntries[i]; |
| 304 | if (E.IsFramework && (E.Group != frontend::Angled || E.IsUserSupplied)) |
| 305 | llvm::llvm_report_error("Invalid option set!"); |
| 306 | if (E.IsUserSupplied) { |
| 307 | if (E.Group == frontend::After) { |
| 308 | Res.push_back("-idirafter"); |
| 309 | } else if (E.Group == frontend::Quoted) { |
| 310 | Res.push_back("-iquoted"); |
| 311 | } else if (E.Group == frontend::System) { |
| 312 | Res.push_back("-isystem"); |
| 313 | } else { |
| 314 | assert(E.Group == frontend::Angled && "Invalid group!"); |
| 315 | Res.push_back(E.IsFramework ? "-F" : "-I"); |
| 316 | } |
| 317 | } else { |
| 318 | if (E.Group != frontend::Angled && E.Group != frontend::System) |
| 319 | llvm::llvm_report_error("Invalid option set!"); |
| 320 | Res.push_back(E.Group == frontend::Angled ? "-iwithprefixbefore" : |
| 321 | "-iwithprefix"); |
| 322 | } |
| 323 | Res.push_back(E.Path); |
| 324 | } |
| 325 | |
| 326 | if (!Opts.EnvIncPath.empty()) { |
| 327 | // FIXME: Provide an option for this, and move env detection to driver. |
| 328 | llvm::llvm_report_error("Not yet implemented!"); |
| 329 | } |
| 330 | if (!Opts.CEnvIncPath.empty()) { |
| 331 | // FIXME: Provide an option for this, and move env detection to driver. |
| 332 | llvm::llvm_report_error("Not yet implemented!"); |
| 333 | } |
| 334 | if (!Opts.ObjCEnvIncPath.empty()) { |
| 335 | // FIXME: Provide an option for this, and move env detection to driver. |
| 336 | llvm::llvm_report_error("Not yet implemented!"); |
| 337 | } |
| 338 | if (!Opts.CXXEnvIncPath.empty()) { |
| 339 | // FIXME: Provide an option for this, and move env detection to driver. |
| 340 | llvm::llvm_report_error("Not yet implemented!"); |
| 341 | } |
| 342 | if (!Opts.ObjCXXEnvIncPath.empty()) { |
| 343 | // FIXME: Provide an option for this, and move env detection to driver. |
| 344 | llvm::llvm_report_error("Not yet implemented!"); |
| 345 | } |
| 346 | if (!Opts.BuiltinIncludePath.empty()) { |
| 347 | // FIXME: Provide an option for this, and move to driver. |
| 348 | } |
| 349 | if (!Opts.UseStandardIncludes) |
| 350 | Res.push_back("-nostdinc"); |
| 351 | if (Opts.Verbose) |
| 352 | Res.push_back("-v"); |
| 353 | } |
| 354 | |
| 355 | static void LangOptsToArgs(const LangOptions &Opts, |
| 356 | std::vector<std::string> &Res) { |
| 357 | LangOptions DefaultLangOpts; |
| 358 | |
| 359 | // FIXME: Need to set -std to get all the implicit options. |
| 360 | |
| 361 | // FIXME: We want to only pass options relative to the defaults, which |
| 362 | // requires constructing a target. :( |
| 363 | // |
| 364 | // It would be better to push the all target specific choices into the driver, |
| 365 | // so that everything below that was more uniform. |
| 366 | |
| 367 | if (Opts.Trigraphs) |
| 368 | Res.push_back("-trigraphs"); |
| 369 | // Implicit based on the input kind: |
| 370 | // AsmPreprocessor, CPlusPlus, ObjC1, ObjC2, OpenCL |
| 371 | // Implicit based on the input language standard: |
| 372 | // BCPLComment, C99, CPlusPlus0x, Digraphs, GNUInline, ImplicitInt, GNUMode |
| 373 | if (Opts.DollarIdents) |
| 374 | Res.push_back("-fdollars-in-identifiers"); |
Daniel Dunbar | a51ca52 | 2009-11-17 09:15:57 +0000 | [diff] [blame] | 375 | if (Opts.Microsoft) |
| 376 | Res.push_back("-fms-extensions=1"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 377 | if (Opts.ObjCNonFragileABI) |
| 378 | Res.push_back("-fobjc-nonfragile-abi"); |
| 379 | // NoInline is implicit. |
| 380 | if (!Opts.CXXOperatorNames) |
| 381 | Res.push_back("-fno-operator-names"); |
| 382 | if (Opts.PascalStrings) |
| 383 | Res.push_back("-fpascal-strings"); |
| 384 | if (Opts.WritableStrings) |
| 385 | Res.push_back("-fwritable-strings"); |
| 386 | if (!Opts.LaxVectorConversions) |
| 387 | Res.push_back("-fno-lax-vector-conversions"); |
| 388 | if (Opts.AltiVec) |
| 389 | Res.push_back("-faltivec"); |
Daniel Dunbar | 1be3b3b | 2009-11-19 20:54:59 +0000 | [diff] [blame] | 390 | if (Opts.Exceptions) |
| 391 | Res.push_back("-fexceptions"); |
Daniel Dunbar | 4db166b | 2009-11-19 05:32:09 +0000 | [diff] [blame] | 392 | if (!Opts.Rtti) |
| 393 | Res.push_back("-fno-rtti"); |
Daniel Dunbar | c5a97ec | 2009-11-17 07:07:28 +0000 | [diff] [blame] | 394 | if (!Opts.NeXTRuntime) |
| 395 | Res.push_back("-fgnu-runtime"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 396 | if (Opts.Freestanding) |
| 397 | Res.push_back("-ffreestanding"); |
| 398 | if (Opts.NoBuiltin) |
| 399 | Res.push_back("-fno-builtin"); |
| 400 | if (Opts.ThreadsafeStatics) |
| 401 | llvm::llvm_report_error("FIXME: Not yet implemented!"); |
| 402 | if (Opts.POSIXThreads) |
| 403 | Res.push_back("-pthread"); |
Daniel Dunbar | a51ca52 | 2009-11-17 09:15:57 +0000 | [diff] [blame] | 404 | if (Opts.Blocks) |
| 405 | Res.push_back("-fblocks=1"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 406 | if (Opts.EmitAllDecls) |
| 407 | Res.push_back("-femit-all-decls"); |
Daniel Dunbar | a51ca52 | 2009-11-17 09:15:57 +0000 | [diff] [blame] | 408 | if (!Opts.MathErrno) |
Daniel Dunbar | 4db166b | 2009-11-19 05:32:09 +0000 | [diff] [blame] | 409 | Res.push_back("-fno-math-errno"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 410 | if (Opts.OverflowChecking) |
| 411 | Res.push_back("-ftrapv"); |
| 412 | if (Opts.HeinousExtensions) |
| 413 | Res.push_back("-fheinous-gnu-extensions"); |
| 414 | // Optimize is implicit. |
| 415 | // OptimizeSize is implicit. |
| 416 | if (Opts.Static) |
| 417 | Res.push_back("-static-define"); |
| 418 | if (Opts.PICLevel) { |
| 419 | Res.push_back("-pic-level"); |
| 420 | Res.push_back(llvm::utostr(Opts.PICLevel)); |
| 421 | } |
| 422 | if (Opts.ObjCGCBitmapPrint) |
| 423 | Res.push_back("-print-ivar-layout"); |
Daniel Dunbar | 1be3b3b | 2009-11-19 20:54:59 +0000 | [diff] [blame] | 424 | // FIXME: Don't forget to update when the default changes! |
| 425 | if (Opts.AccessControl) |
| 426 | Res.push_back("-faccess-control"); |
| 427 | if (!Opts.CharIsSigned) |
| 428 | Res.push_back("-fsigned-char=0"); |
| 429 | if (Opts.ShortWChar) |
| 430 | Res.push_back("-fshort-wchar"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 431 | if (!Opts.ElideConstructors) |
| 432 | Res.push_back("-fno-elide-constructors"); |
| 433 | if (Opts.getGCMode() != LangOptions::NonGC) { |
| 434 | if (Opts.getGCMode() == LangOptions::HybridGC) { |
| 435 | Res.push_back("-fobjc-gc"); |
| 436 | } else { |
| 437 | assert(Opts.getGCMode() == LangOptions::GCOnly && "Invalid GC mode!"); |
| 438 | Res.push_back("-fobjc-gc-only"); |
| 439 | } |
| 440 | } |
| 441 | if (Opts.getVisibilityMode() != LangOptions::Default) { |
| 442 | Res.push_back("-fvisibility"); |
| 443 | if (Opts.getVisibilityMode() == LangOptions::Hidden) { |
Daniel Dunbar | 1be3b3b | 2009-11-19 20:54:59 +0000 | [diff] [blame] | 444 | Res.push_back("hidden"); |
Daniel Dunbar | b3375cb | 2009-11-17 06:02:29 +0000 | [diff] [blame] | 445 | } else { |
| 446 | assert(Opts.getVisibilityMode() == LangOptions::Protected && |
| 447 | "Invalid visibility!"); |
| 448 | Res.push_back("protected"); |
| 449 | } |
| 450 | } |
| 451 | if (Opts.getStackProtectorMode() != 0) { |
| 452 | Res.push_back("-stack-protector"); |
| 453 | Res.push_back(llvm::utostr(Opts.getStackProtectorMode())); |
| 454 | } |
| 455 | if (Opts.getMainFileName()) { |
| 456 | Res.push_back("-main-file-name"); |
| 457 | Res.push_back(Opts.getMainFileName()); |
| 458 | } |
| 459 | if (Opts.InstantiationDepth != DefaultLangOpts.InstantiationDepth) { |
| 460 | Res.push_back("-ftemplate-depth"); |
| 461 | Res.push_back(llvm::utostr(Opts.InstantiationDepth)); |
| 462 | } |
| 463 | if (Opts.ObjCConstantStringClass) { |
| 464 | Res.push_back("-fconstant-string-class"); |
| 465 | Res.push_back(Opts.ObjCConstantStringClass); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static void PreprocessorOptsToArgs(const PreprocessorOptions &Opts, |
| 470 | std::vector<std::string> &Res) { |
| 471 | for (unsigned i = 0, e = Opts.Macros.size(); i != e; ++i) |
| 472 | Res.push_back((Opts.Macros[i].second ? "-U" : "-D") + Opts.Macros[i].first); |
| 473 | for (unsigned i = 0, e = Opts.Includes.size(); i != e; ++i) { |
| 474 | Res.push_back("-include"); |
| 475 | Res.push_back(Opts.Includes[i]); |
| 476 | } |
| 477 | for (unsigned i = 0, e = Opts.MacroIncludes.size(); i != e; ++i) { |
| 478 | Res.push_back("-imacros"); |
| 479 | Res.push_back(Opts.Includes[i]); |
| 480 | } |
| 481 | if (!Opts.UsePredefines) |
| 482 | Res.push_back("-undef"); |
| 483 | if (!Opts.ImplicitPCHInclude.empty()) { |
| 484 | Res.push_back("-implicit-pch-include"); |
| 485 | Res.push_back(Opts.ImplicitPCHInclude); |
| 486 | } |
| 487 | if (!Opts.ImplicitPTHInclude.empty()) { |
| 488 | Res.push_back("-implicit-pth-include"); |
| 489 | Res.push_back(Opts.ImplicitPTHInclude); |
| 490 | } |
| 491 | if (!Opts.TokenCache.empty()) { |
| 492 | Res.push_back("-token-cache"); |
| 493 | Res.push_back(Opts.TokenCache); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | static void PreprocessorOutputOptsToArgs(const PreprocessorOutputOptions &Opts, |
| 498 | std::vector<std::string> &Res) { |
| 499 | if (!Opts.ShowCPP && !Opts.ShowMacros) |
| 500 | llvm::llvm_report_error("Invalid option combination!"); |
| 501 | |
| 502 | if (Opts.ShowCPP && Opts.ShowMacros) |
| 503 | Res.push_back("-dD"); |
| 504 | else if (!Opts.ShowCPP && Opts.ShowMacros) |
| 505 | Res.push_back("-dM"); |
| 506 | |
| 507 | if (!Opts.ShowLineMarkers) |
| 508 | Res.push_back("-P"); |
| 509 | if (Opts.ShowComments) |
| 510 | Res.push_back("-C"); |
| 511 | if (Opts.ShowMacroComments) |
| 512 | Res.push_back("-CC"); |
| 513 | } |
| 514 | |
| 515 | static void TargetOptsToArgs(const TargetOptions &Opts, |
| 516 | std::vector<std::string> &Res) { |
| 517 | Res.push_back("-triple"); |
| 518 | Res.push_back(Opts.Triple); |
| 519 | if (!Opts.CPU.empty()) { |
| 520 | Res.push_back("-target-cpu"); |
| 521 | Res.push_back(Opts.CPU); |
| 522 | } |
| 523 | if (!Opts.ABI.empty()) { |
| 524 | Res.push_back("-target-abi"); |
| 525 | Res.push_back(Opts.ABI); |
| 526 | } |
| 527 | for (unsigned i = 0, e = Opts.Features.size(); i != e; ++i) { |
| 528 | Res.push_back("-target-feature"); |
| 529 | Res.push_back(Opts.Features[i]); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void CompilerInvocation::toArgs(std::vector<std::string> &Res) { |
| 534 | AnalyzerOptsToArgs(getAnalyzerOpts(), Res); |
| 535 | CodeGenOptsToArgs(getCodeGenOpts(), Res); |
| 536 | DependencyOutputOptsToArgs(getDependencyOutputOpts(), Res); |
| 537 | DiagnosticOptsToArgs(getDiagnosticOpts(), Res); |
| 538 | FrontendOptsToArgs(getFrontendOpts(), Res); |
| 539 | HeaderSearchOptsToArgs(getHeaderSearchOpts(), Res); |
| 540 | LangOptsToArgs(getLangOpts(), Res); |
| 541 | PreprocessorOptsToArgs(getPreprocessorOpts(), Res); |
| 542 | PreprocessorOutputOptsToArgs(getPreprocessorOutputOpts(), Res); |
| 543 | TargetOptsToArgs(getTargetOpts(), Res); |
| 544 | } |