blob: 6b5c8cf5b568ad768cda506c51b319282f059ebb [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"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000011#include "clang/Basic/Diagnostic.h"
12#include "clang/Basic/Version.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000013#include "clang/Basic/FileManager.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000014#include "clang/Driver/Arg.h"
15#include "clang/Driver/ArgList.h"
16#include "clang/Driver/CC1Options.h"
17#include "clang/Driver/DriverDiagnostic.h"
18#include "clang/Driver/OptTable.h"
19#include "clang/Driver/Option.h"
20#include "clang/Frontend/CompilerInvocation.h"
21#include "clang/Frontend/LangStandard.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000022#include "clang/Serialization/ASTReader.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000023#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/SmallVector.h"
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000025#include "llvm/ADT/StringExtras.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000026#include "llvm/ADT/StringSwitch.h"
Duncan Sands2dc14532010-08-30 09:42:39 +000027#include "llvm/ADT/Triple.h"
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000028#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Host.h"
30#include "llvm/Support/Path.h"
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000031using namespace clang;
32
Ted Kremenekd3b74d92011-11-17 23:01:24 +000033//===----------------------------------------------------------------------===//
34// Initialization.
35//===----------------------------------------------------------------------===//
36
Ted Kremenek8a0c5c22011-11-18 04:32:13 +000037CompilerInvocationBase::CompilerInvocationBase()
Ted Kremenekd3b74d92011-11-17 23:01:24 +000038 : LangOpts(new LangOptions()) {}
39
Ted Kremenek8a0c5c22011-11-18 04:32:13 +000040CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &X)
Benjamin Kramer85224ae2011-11-29 11:31:35 +000041 : llvm::RefCountedBase<CompilerInvocation>(),
42 LangOpts(new LangOptions(*X.getLangOpts())) {}
Ted Kremenekd3b74d92011-11-17 23:01:24 +000043
44//===----------------------------------------------------------------------===//
45// Utility functions.
46//===----------------------------------------------------------------------===//
47
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000048static const char *getAnalysisStoreName(AnalysisStores Kind) {
49 switch (Kind) {
50 default:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000051 llvm_unreachable("Unknown analysis store!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000052#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) \
53 case NAME##Model: return CMDFLAG;
54#include "clang/Frontend/Analyses.def"
55 }
56}
57
58static const char *getAnalysisConstraintName(AnalysisConstraints Kind) {
59 switch (Kind) {
60 default:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000061 llvm_unreachable("Unknown analysis constraints!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000062#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) \
63 case NAME##Model: return CMDFLAG;
64#include "clang/Frontend/Analyses.def"
65 }
66}
67
68static const char *getAnalysisDiagClientName(AnalysisDiagClients Kind) {
69 switch (Kind) {
70 default:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000071 llvm_unreachable("Unknown analysis client!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000072#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE) \
73 case PD_##NAME: return CMDFLAG;
74#include "clang/Frontend/Analyses.def"
75 }
76}
77
Anna Zaksd3095282011-09-30 02:03:00 +000078static const char *getAnalysisPurgeModeName(AnalysisPurgeMode Kind) {
79 switch (Kind) {
80 default:
81 llvm_unreachable("Unknown analysis client!");
82#define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) \
83 case NAME: return CMDFLAG;
84#include "clang/Frontend/Analyses.def"
85 }
86}
87
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000088//===----------------------------------------------------------------------===//
89// Serialization (to args)
90//===----------------------------------------------------------------------===//
91
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000092static void AnalyzerOptsToArgs(const AnalyzerOptions &Opts,
93 std::vector<std::string> &Res) {
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +000094 if (Opts.ShowCheckerHelp)
95 Res.push_back("-analyzer-checker-help");
Ted Kremeneka4c7a432011-07-28 23:08:16 +000096 if (Opts.AnalysisStoreOpt != RegionStoreModel) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000097 Res.push_back("-analyzer-store");
98 Res.push_back(getAnalysisStoreName(Opts.AnalysisStoreOpt));
99 }
100 if (Opts.AnalysisConstraintsOpt != RangeConstraintsModel) {
101 Res.push_back("-analyzer-constraints");
102 Res.push_back(getAnalysisConstraintName(Opts.AnalysisConstraintsOpt));
103 }
104 if (Opts.AnalysisDiagOpt != PD_HTML) {
105 Res.push_back("-analyzer-output");
106 Res.push_back(getAnalysisDiagClientName(Opts.AnalysisDiagOpt));
107 }
Anna Zaksd3095282011-09-30 02:03:00 +0000108 if (Opts.AnalysisPurgeOpt != PurgeStmt) {
109 Res.push_back("-analyzer-purge");
110 Res.push_back(getAnalysisPurgeModeName(Opts.AnalysisPurgeOpt));
111 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000112 if (!Opts.AnalyzeSpecificFunction.empty()) {
113 Res.push_back("-analyze-function");
114 Res.push_back(Opts.AnalyzeSpecificFunction);
115 }
116 if (Opts.AnalyzeAll)
117 Res.push_back("-analyzer-opt-analyze-headers");
118 if (Opts.AnalyzerDisplayProgress)
119 Res.push_back("-analyzer-display-progress");
Ted Kremenekfc576512009-12-07 22:06:12 +0000120 if (Opts.AnalyzeNestedBlocks)
121 Res.push_back("-analyzer-opt-analyze-nested-blocks");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000122 if (Opts.EagerlyAssume)
123 Res.push_back("-analyzer-eagerly-assume");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000124 if (Opts.TrimGraph)
125 Res.push_back("-trim-egraph");
126 if (Opts.VisualizeEGDot)
127 Res.push_back("-analyzer-viz-egraph-graphviz");
Sandeep Patel1040c222011-03-16 19:14:34 +0000128 if (Opts.VisualizeEGUbi)
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000129 Res.push_back("-analyzer-viz-egraph-ubigraph");
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000130
131 for (unsigned i = 0, e = Opts.CheckersControlList.size(); i != e; ++i) {
132 const std::pair<std::string, bool> &opt = Opts.CheckersControlList[i];
133 if (opt.second)
134 Res.push_back("-analyzer-disable-checker");
135 else
136 Res.push_back("-analyzer-checker");
137 Res.push_back(opt.first);
138 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000139}
140
141static void CodeGenOptsToArgs(const CodeGenOptions &Opts,
142 std::vector<std::string> &Res) {
143 if (Opts.DebugInfo)
144 Res.push_back("-g");
145 if (Opts.DisableLLVMOpts)
146 Res.push_back("-disable-llvm-optzns");
147 if (Opts.DisableRedZone)
148 Res.push_back("-disable-red-zone");
Nick Lewycky7c4fd912011-10-21 02:32:14 +0000149 if (!Opts.DebugCompilationDir.empty()) {
150 Res.push_back("-fdebug-compilation-dir");
151 Res.push_back(Opts.DebugCompilationDir);
152 }
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000153 if (!Opts.DwarfDebugFlags.empty()) {
154 Res.push_back("-dwarf-debug-flags");
155 Res.push_back(Opts.DwarfDebugFlags);
156 }
John McCall9f084a32011-07-06 00:26:06 +0000157 if (Opts.ObjCRuntimeHasARC)
158 Res.push_back("-fobjc-runtime-has-arc");
John McCall256a76e2011-07-06 01:22:26 +0000159 if (Opts.ObjCRuntimeHasTerminate)
160 Res.push_back("-fobjc-runtime-has-terminate");
Nick Lewyckye8ba8d72011-04-21 23:44:07 +0000161 if (Opts.EmitGcovArcs)
162 Res.push_back("-femit-coverage-data");
163 if (Opts.EmitGcovNotes)
164 Res.push_back("-femit-coverage-notes");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000165 if (!Opts.MergeAllConstants)
166 Res.push_back("-fno-merge-all-constants");
Daniel Dunbar50a44872009-11-20 17:23:30 +0000167 if (Opts.NoCommon)
168 Res.push_back("-fno-common");
John McCall32096692011-03-18 02:56:14 +0000169 if (Opts.ForbidGuardVariables)
170 Res.push_back("-fforbid-guard-variables");
Daniel Dunbare26bdb92011-06-21 18:54:46 +0000171 if (Opts.UseRegisterSizedBitfieldAccess)
172 Res.push_back("-fuse-register-sized-bitfield-access");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000173 if (Opts.NoImplicitFloat)
174 Res.push_back("-no-implicit-float");
Daniel Dunbar1ad66482010-07-01 01:31:45 +0000175 if (Opts.OmitLeafFramePointer)
176 Res.push_back("-momit-leaf-frame-pointer");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000177 if (Opts.OptimizeSize) {
178 assert(Opts.OptimizationLevel == 2 && "Invalid options!");
Bob Wilsona0fa2032011-04-29 22:49:50 +0000179 Opts.OptimizeSize == 1 ? Res.push_back("-Os") : Res.push_back("-Oz");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000180 } else if (Opts.OptimizationLevel != 0)
181 Res.push_back("-O" + llvm::utostr(Opts.OptimizationLevel));
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000182 if (!Opts.MainFileName.empty()) {
183 Res.push_back("-main-file-name");
184 Res.push_back(Opts.MainFileName);
185 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000186 // SimplifyLibCalls is only derived.
187 // TimePasses is only derived.
188 // UnitAtATime is unused.
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000189 // Inlining is only derived.
Michael J. Spencer20249a12010-10-21 03:16:25 +0000190
Eric Christopherf84d4092010-08-07 23:08:14 +0000191 // UnrollLoops is derived, but also accepts an option, no
192 // harm in pushing it back here.
193 if (Opts.UnrollLoops)
194 Res.push_back("-funroll-loops");
Chris Lattnerbbea7162010-04-13 00:38:24 +0000195 if (Opts.DataSections)
196 Res.push_back("-fdata-sections");
197 if (Opts.FunctionSections)
198 Res.push_back("-ffunction-sections");
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000199 if (Opts.AsmVerbose)
200 Res.push_back("-masm-verbose");
201 if (!Opts.CodeModel.empty()) {
202 Res.push_back("-mcode-model");
203 Res.push_back(Opts.CodeModel);
204 }
Peter Collingbourned51e43a2011-10-06 18:29:46 +0000205 if (Opts.CUDAIsDevice)
206 Res.push_back("-fcuda-is-device");
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000207 if (!Opts.CXAAtExit)
208 Res.push_back("-fno-use-cxa-atexit");
209 if (Opts.CXXCtorDtorAliases)
210 Res.push_back("-mconstructor-aliases");
John McCallf85e1932011-06-15 23:02:42 +0000211 if (Opts.ObjCAutoRefCountExceptions)
212 Res.push_back("-fobjc-arc-eh");
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000213 if (!Opts.DebugPass.empty()) {
214 Res.push_back("-mdebug-pass");
215 Res.push_back(Opts.DebugPass);
216 }
217 if (Opts.DisableFPElim)
218 Res.push_back("-mdisable-fp-elim");
Daniel Dunbar3b315262009-11-30 08:42:00 +0000219 if (!Opts.FloatABI.empty()) {
220 Res.push_back("-mfloat-abi");
221 Res.push_back(Opts.FloatABI);
222 }
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000223 if (!Opts.LimitFloatPrecision.empty()) {
224 Res.push_back("-mlimit-float-precision");
225 Res.push_back(Opts.LimitFloatPrecision);
226 }
227 if (Opts.NoZeroInitializedInBSS)
228 Res.push_back("-mno-zero-initialized-bss");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +0000229 switch (Opts.getObjCDispatchMethod()) {
230 case CodeGenOptions::Legacy:
231 break;
232 case CodeGenOptions::Mixed:
233 Res.push_back("-fobjc-dispatch-method=mixed");
234 break;
235 case CodeGenOptions::NonLegacy:
236 Res.push_back("-fobjc-dispatch-method=non-legacy");
237 break;
238 }
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000239 if (Opts.NumRegisterParameters) {
240 Res.push_back("-mregparm");
241 Res.push_back(llvm::utostr(Opts.NumRegisterParameters));
242 }
Chad Rosier1b906052011-08-26 00:26:29 +0000243 if (Opts.NoGlobalMerge)
244 Res.push_back("-mno-global-merge");
Nick Lewyckyc3b90142011-06-21 00:14:18 +0000245 if (Opts.NoExecStack)
246 Res.push_back("-mnoexecstack");
Daniel Dunbar4ecc9b72010-05-27 05:39:39 +0000247 if (Opts.RelaxAll)
248 Res.push_back("-mrelax-all");
Daniel Dunbar96932322011-03-28 22:49:28 +0000249 if (Opts.SaveTempLabels)
250 Res.push_back("-msave-temp-labels");
Rafael Espindolaf24a1512011-04-30 18:35:43 +0000251 if (Opts.NoDwarf2CFIAsm)
252 Res.push_back("-fno-dwarf2-cfi-asm");
Nick Lewyckyea523d72011-10-17 23:05:52 +0000253 if (Opts.NoDwarfDirectoryAsm)
254 Res.push_back("-fno-dwarf-directory-asm");
Daniel Dunbar3b315262009-11-30 08:42:00 +0000255 if (Opts.SoftFloat)
256 Res.push_back("-msoft-float");
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000257 if (Opts.UnwindTables)
258 Res.push_back("-munwind-tables");
259 if (Opts.RelocationModel != "pic") {
260 Res.push_back("-mrelocation-model");
261 Res.push_back(Opts.RelocationModel);
262 }
John McCallb689afb2010-02-13 03:50:24 +0000263 if (!Opts.VerifyModule)
264 Res.push_back("-disable-llvm-verifier");
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000265 for (unsigned i = 0, e = Opts.BackendOptions.size(); i != e; ++i) {
266 Res.push_back("-backend-option");
267 Res.push_back(Opts.BackendOptions[i]);
268 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000269}
270
271static void DependencyOutputOptsToArgs(const DependencyOutputOptions &Opts,
272 std::vector<std::string> &Res) {
273 if (Opts.IncludeSystemHeaders)
274 Res.push_back("-sys-header-deps");
Daniel Dunbareef63e02011-02-02 15:41:17 +0000275 if (Opts.ShowHeaderIncludes)
276 Res.push_back("-H");
Daniel Dunbarb34d69b2011-02-02 21:11:31 +0000277 if (!Opts.HeaderIncludeOutputFile.empty()) {
278 Res.push_back("-header-include-file");
279 Res.push_back(Opts.HeaderIncludeOutputFile);
280 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000281 if (Opts.UsePhonyTargets)
282 Res.push_back("-MP");
283 if (!Opts.OutputFile.empty()) {
284 Res.push_back("-dependency-file");
285 Res.push_back(Opts.OutputFile);
286 }
287 for (unsigned i = 0, e = Opts.Targets.size(); i != e; ++i) {
288 Res.push_back("-MT");
289 Res.push_back(Opts.Targets[i]);
290 }
291}
292
293static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts,
294 std::vector<std::string> &Res) {
295 if (Opts.IgnoreWarnings)
296 Res.push_back("-w");
297 if (Opts.NoRewriteMacros)
298 Res.push_back("-Wno-rewrite-macros");
299 if (Opts.Pedantic)
300 Res.push_back("-pedantic");
301 if (Opts.PedanticErrors)
302 Res.push_back("-pedantic-errors");
303 if (!Opts.ShowColumn)
304 Res.push_back("-fno-show-column");
305 if (!Opts.ShowLocation)
306 Res.push_back("-fno-show-source-location");
307 if (!Opts.ShowCarets)
308 Res.push_back("-fno-caret-diagnostics");
309 if (!Opts.ShowFixits)
310 Res.push_back("-fno-diagnostics-fixit-info");
311 if (Opts.ShowSourceRanges)
312 Res.push_back("-fdiagnostics-print-source-range-info");
Douglas Gregor4786c152010-08-19 20:24:43 +0000313 if (Opts.ShowParseableFixits)
314 Res.push_back("-fdiagnostics-parseable-fixits");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000315 if (Opts.ShowColors)
316 Res.push_back("-fcolor-diagnostics");
317 if (Opts.VerifyDiagnostics)
318 Res.push_back("-verify");
Douglas Gregor7d2b8c12011-04-15 22:04:17 +0000319 if (Opts.ShowNames)
320 Res.push_back("-fdiagnostics-show-name");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000321 if (Opts.ShowOptionNames)
322 Res.push_back("-fdiagnostics-show-option");
Chris Lattner6fbe8392010-05-04 21:55:25 +0000323 if (Opts.ShowCategories == 1)
324 Res.push_back("-fdiagnostics-show-category=id");
325 else if (Opts.ShowCategories == 2)
326 Res.push_back("-fdiagnostics-show-category=name");
Douglas Gregorc9471b02011-05-21 17:07:29 +0000327 switch (Opts.Format) {
328 case DiagnosticOptions::Clang:
329 Res.push_back("-fdiagnostics-format=clang"); break;
330 case DiagnosticOptions::Msvc:
331 Res.push_back("-fdiagnostics-format=msvc"); break;
332 case DiagnosticOptions::Vi:
333 Res.push_back("-fdiagnostics-format=vi"); break;
334 }
Chris Lattnerc1002142010-04-07 20:37:06 +0000335 if (Opts.ErrorLimit) {
336 Res.push_back("-ferror-limit");
337 Res.push_back(llvm::utostr(Opts.ErrorLimit));
338 }
Daniel Dunbar08c66952011-04-07 18:11:14 +0000339 if (!Opts.DiagnosticLogFile.empty()) {
340 Res.push_back("-diagnostic-log-file");
341 Res.push_back(Opts.DiagnosticLogFile);
342 }
Douglas Gregor6c1cb992010-05-04 17:13:42 +0000343 if (Opts.MacroBacktraceLimit
344 != DiagnosticOptions::DefaultMacroBacktraceLimit) {
345 Res.push_back("-fmacro-backtrace-limit");
346 Res.push_back(llvm::utostr(Opts.MacroBacktraceLimit));
347 }
348 if (Opts.TemplateBacktraceLimit
349 != DiagnosticOptions::DefaultTemplateBacktraceLimit) {
Douglas Gregor575cf372010-04-20 07:18:24 +0000350 Res.push_back("-ftemplate-backtrace-limit");
351 Res.push_back(llvm::utostr(Opts.TemplateBacktraceLimit));
352 }
353
Chris Lattner52388f92010-01-13 03:06:50 +0000354 if (Opts.TabStop != DiagnosticOptions::DefaultTabStop) {
Chris Lattner124fca52010-01-09 21:54:33 +0000355 Res.push_back("-ftabstop");
356 Res.push_back(llvm::utostr(Opts.TabStop));
357 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000358 if (Opts.MessageLength) {
359 Res.push_back("-fmessage-length");
360 Res.push_back(llvm::utostr(Opts.MessageLength));
361 }
362 if (!Opts.DumpBuildInformation.empty()) {
363 Res.push_back("-dump-build-information");
364 Res.push_back(Opts.DumpBuildInformation);
365 }
366 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i)
367 Res.push_back("-W" + Opts.Warnings[i]);
368}
369
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000370static const char *getInputKindName(InputKind Kind) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000371 switch (Kind) {
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000372 case IK_None: break;
373 case IK_AST: return "ast";
374 case IK_Asm: return "assembler-with-cpp";
375 case IK_C: return "c";
376 case IK_CXX: return "c++";
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000377 case IK_LLVM_IR: return "ir";
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000378 case IK_ObjC: return "objective-c";
379 case IK_ObjCXX: return "objective-c++";
380 case IK_OpenCL: return "cl";
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000381 case IK_CUDA: return "cuda";
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000382 case IK_PreprocessedC: return "cpp-output";
383 case IK_PreprocessedCXX: return "c++-cpp-output";
384 case IK_PreprocessedObjC: return "objective-c-cpp-output";
385 case IK_PreprocessedObjCXX:return "objective-c++-cpp-output";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000386 }
387
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000388 llvm_unreachable("Unexpected language kind!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000389 return 0;
390}
391
392static const char *getActionName(frontend::ActionKind Kind) {
393 switch (Kind) {
394 case frontend::PluginAction:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000395 llvm_unreachable("Invalid kind!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000396
397 case frontend::ASTDump: return "-ast-dump";
John McCallf3514242010-11-24 11:21:45 +0000398 case frontend::ASTDumpXML: return "-ast-dump-xml";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000399 case frontend::ASTPrint: return "-ast-print";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000400 case frontend::ASTView: return "-ast-view";
401 case frontend::DumpRawTokens: return "-dump-raw-tokens";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000402 case frontend::DumpTokens: return "-dump-tokens";
403 case frontend::EmitAssembly: return "-S";
404 case frontend::EmitBC: return "-emit-llvm-bc";
405 case frontend::EmitHTML: return "-emit-html";
406 case frontend::EmitLLVM: return "-emit-llvm";
407 case frontend::EmitLLVMOnly: return "-emit-llvm-only";
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000408 case frontend::EmitCodeGenOnly: return "-emit-codegen-only";
Daniel Dunbarda1573f2010-02-03 01:18:43 +0000409 case frontend::EmitObj: return "-emit-obj";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000410 case frontend::FixIt: return "-fixit";
Douglas Gregor467dc882011-08-25 22:30:56 +0000411 case frontend::GenerateModule: return "-emit-module";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000412 case frontend::GeneratePCH: return "-emit-pch";
413 case frontend::GeneratePTH: return "-emit-pth";
Daniel Dunbar27585952010-03-19 19:44:04 +0000414 case frontend::InitOnly: return "-init-only";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000415 case frontend::ParseSyntaxOnly: return "-fsyntax-only";
416 case frontend::PrintDeclContext: return "-print-decl-contexts";
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000417 case frontend::PrintPreamble: return "-print-preamble";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000418 case frontend::PrintPreprocessedInput: return "-E";
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000419 case frontend::RewriteMacros: return "-rewrite-macros";
420 case frontend::RewriteObjC: return "-rewrite-objc";
421 case frontend::RewriteTest: return "-rewrite-test";
422 case frontend::RunAnalysis: return "-analyze";
423 case frontend::RunPreprocessorOnly: return "-Eonly";
424 }
425
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000426 llvm_unreachable("Unexpected language kind!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000427 return 0;
428}
429
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000430static void FileSystemOptsToArgs(const FileSystemOptions &Opts,
431 std::vector<std::string> &Res) {
432 if (!Opts.WorkingDir.empty()) {
433 Res.push_back("-working-directory");
434 Res.push_back(Opts.WorkingDir);
435 }
436}
437
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000438static void FrontendOptsToArgs(const FrontendOptions &Opts,
439 std::vector<std::string> &Res) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000440 if (Opts.DisableFree)
441 Res.push_back("-disable-free");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000442 if (Opts.RelocatablePCH)
443 Res.push_back("-relocatable-pch");
Daniel Dunbar9b5e9ae2009-12-03 07:01:58 +0000444 if (Opts.ShowHelp)
445 Res.push_back("-help");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000446 if (Opts.ShowMacrosInCodeCompletion)
447 Res.push_back("-code-completion-macros");
Douglas Gregord8e8a582010-05-25 21:41:55 +0000448 if (Opts.ShowCodePatternsInCodeCompletion)
449 Res.push_back("-code-completion-patterns");
Douglas Gregor8071e422010-08-15 06:18:01 +0000450 if (!Opts.ShowGlobalSymbolsInCodeCompletion)
451 Res.push_back("-no-code-completion-globals");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000452 if (Opts.ShowStats)
Daniel Dunbareb01ac82009-11-25 10:14:52 +0000453 Res.push_back("-print-stats");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000454 if (Opts.ShowTimers)
455 Res.push_back("-ftime-report");
Daniel Dunbar9b5e9ae2009-12-03 07:01:58 +0000456 if (Opts.ShowVersion)
457 Res.push_back("-version");
Nick Lewycky1450f262010-08-13 17:31:00 +0000458 if (Opts.FixWhatYouCan)
459 Res.push_back("-fix-what-you-can");
John McCall8f0e8d22011-06-15 23:25:17 +0000460 switch (Opts.ARCMTAction) {
461 case FrontendOptions::ARCMT_None:
462 break;
463 case FrontendOptions::ARCMT_Check:
464 Res.push_back("-arcmt-check");
465 break;
466 case FrontendOptions::ARCMT_Modify:
467 Res.push_back("-arcmt-modify");
468 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000469 case FrontendOptions::ARCMT_Migrate:
470 Res.push_back("-arcmt-migrate");
471 break;
472 }
473 if (!Opts.ARCMTMigrateDir.empty()) {
474 Res.push_back("-arcmt-migrate-directory");
475 Res.push_back(Opts.ARCMTMigrateDir);
John McCall8f0e8d22011-06-15 23:25:17 +0000476 }
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000477 if (!Opts.ARCMTMigrateReportOut.empty()) {
478 Res.push_back("-arcmt-migrate-report-output");
479 Res.push_back(Opts.ARCMTMigrateReportOut);
480 }
481 if (Opts.ARCMTMigrateEmitARCErrors)
482 Res.push_back("-arcmt-migrate-emit-errors");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000483
484 bool NeedLang = false;
485 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i)
486 if (FrontendOptions::getInputKindForExtension(Opts.Inputs[i].second) !=
487 Opts.Inputs[i].first)
488 NeedLang = true;
489 if (NeedLang) {
490 Res.push_back("-x");
491 Res.push_back(getInputKindName(Opts.Inputs[0].first));
492 }
493 for (unsigned i = 0, e = Opts.Inputs.size(); i != e; ++i) {
494 assert((!NeedLang || Opts.Inputs[i].first == Opts.Inputs[0].first) &&
495 "Unable to represent this input vector!");
496 Res.push_back(Opts.Inputs[i].second);
497 }
498
499 if (!Opts.OutputFile.empty()) {
500 Res.push_back("-o");
501 Res.push_back(Opts.OutputFile);
502 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000503 if (!Opts.CodeCompletionAt.FileName.empty()) {
504 Res.push_back("-code-completion-at");
505 Res.push_back(Opts.CodeCompletionAt.FileName + ":" +
506 llvm::utostr(Opts.CodeCompletionAt.Line) + ":" +
507 llvm::utostr(Opts.CodeCompletionAt.Column));
508 }
Douglas Gregor91a230a2011-02-17 19:04:40 +0000509 if (Opts.ProgramAction != frontend::PluginAction)
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000510 Res.push_back(getActionName(Opts.ProgramAction));
511 if (!Opts.ActionName.empty()) {
512 Res.push_back("-plugin");
513 Res.push_back(Opts.ActionName);
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000514 for(unsigned i = 0, e = Opts.PluginArgs.size(); i != e; ++i) {
515 Res.push_back("-plugin-arg-" + Opts.ActionName);
516 Res.push_back(Opts.PluginArgs[i]);
517 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000518 }
Daniel Dunbarefba2272009-12-03 05:11:05 +0000519 for (unsigned i = 0, e = Opts.Plugins.size(); i != e; ++i) {
520 Res.push_back("-load");
521 Res.push_back(Opts.Plugins[i]);
522 }
Nico Weber5aa74af2011-01-25 20:34:14 +0000523 for (unsigned i = 0, e = Opts.AddPluginActions.size(); i != e; ++i) {
524 Res.push_back("-add-plugin");
525 Res.push_back(Opts.AddPluginActions[i]);
Nico Weberf25649c2011-01-29 21:21:49 +0000526 for(unsigned ai = 0, ae = Opts.AddPluginArgs.size(); ai != ae; ++ai) {
527 Res.push_back("-plugin-arg-" + Opts.AddPluginActions[i]);
528 Res.push_back(Opts.AddPluginArgs[i][ai]);
529 }
Nico Weber5aa74af2011-01-25 20:34:14 +0000530 }
Douglas Gregor9bed8792010-02-09 19:21:46 +0000531 for (unsigned i = 0, e = Opts.ASTMergeFiles.size(); i != e; ++i) {
532 Res.push_back("-ast-merge");
533 Res.push_back(Opts.ASTMergeFiles[i]);
534 }
Daniel Dunbar3f87fb02010-04-15 06:09:03 +0000535 for (unsigned i = 0, e = Opts.LLVMArgs.size(); i != e; ++i) {
536 Res.push_back("-mllvm");
537 Res.push_back(Opts.LLVMArgs[i]);
538 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000539}
540
541static void HeaderSearchOptsToArgs(const HeaderSearchOptions &Opts,
542 std::vector<std::string> &Res) {
Daniel Dunbarc59dc922009-11-19 05:32:21 +0000543 if (Opts.Sysroot != "/") {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000544 Res.push_back("-isysroot");
545 Res.push_back(Opts.Sysroot);
546 }
547
548 /// User specified include entries.
549 for (unsigned i = 0, e = Opts.UserEntries.size(); i != e; ++i) {
550 const HeaderSearchOptions::Entry &E = Opts.UserEntries[i];
Daniel Dunbard8c78062009-11-26 02:13:54 +0000551 if (E.IsFramework && (E.Group != frontend::Angled || !E.IsUserSupplied))
Chris Lattner83e7a782010-04-07 22:58:06 +0000552 llvm::report_fatal_error("Invalid option set!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000553 if (E.IsUserSupplied) {
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000554 switch (E.Group) {
555 case frontend::After:
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000556 Res.push_back("-idirafter");
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000557 break;
558
559 case frontend::Quoted:
Daniel Dunbard8c78062009-11-26 02:13:54 +0000560 Res.push_back("-iquote");
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000561 break;
562
563 case frontend::System:
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000564 Res.push_back("-isystem");
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000565 break;
566
567 case frontend::IndexHeaderMap:
568 Res.push_back("-index-header-map");
569 Res.push_back(E.IsFramework? "-F" : "-I");
570 break;
571
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000572 case frontend::CSystem:
573 Res.push_back("-c-isystem");
574 break;
575
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000576 case frontend::CXXSystem:
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000577 Res.push_back("-cxx-isystem");
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000578 break;
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000579
580 case frontend::ObjCSystem:
581 Res.push_back("-objc-isystem");
582 break;
583
584 case frontend::ObjCXXSystem:
585 Res.push_back("-objcxx-isystem");
586 break;
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000587
588 case frontend::Angled:
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000589 Res.push_back(E.IsFramework ? "-F" : "-I");
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000590 break;
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000591 }
592 } else {
Chandler Carruthac2bc4d2011-11-05 08:30:29 +0000593 if (E.IsInternal) {
594 assert(E.Group == frontend::System && "Unexpected header search group");
Chandler Carruth07643082011-11-07 09:17:31 +0000595 if (E.ImplicitExternC)
Chandler Carruth103accc2011-11-05 09:24:44 +0000596 Res.push_back("-internal-externc-isystem");
597 else
598 Res.push_back("-internal-isystem");
Chandler Carruthac2bc4d2011-11-05 08:30:29 +0000599 } else {
600 if (E.Group != frontend::Angled && E.Group != frontend::System)
601 llvm::report_fatal_error("Invalid option set!");
602 Res.push_back(E.Group == frontend::Angled ? "-iwithprefixbefore" :
603 "-iwithprefix");
604 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000605 }
606 Res.push_back(E.Path);
607 }
608
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000609 if (!Opts.ResourceDir.empty()) {
610 Res.push_back("-resource-dir");
611 Res.push_back(Opts.ResourceDir);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000612 }
Douglas Gregor9a6da692011-09-12 20:41:59 +0000613 if (!Opts.ModuleCachePath.empty()) {
614 Res.push_back("-fmodule-cache-path");
615 Res.push_back(Opts.ModuleCachePath);
616 }
Daniel Dunbara268fc02011-10-11 18:20:10 +0000617 if (!Opts.UseStandardSystemIncludes)
618 Res.push_back("-nostdsysteminc");
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000619 if (!Opts.UseStandardCXXIncludes)
620 Res.push_back("-nostdinc++");
Bob Wilson13c4f212011-06-21 21:12:29 +0000621 if (Opts.UseLibcxx)
622 Res.push_back("-stdlib=libc++");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000623 if (Opts.Verbose)
624 Res.push_back("-v");
625}
626
627static void LangOptsToArgs(const LangOptions &Opts,
628 std::vector<std::string> &Res) {
629 LangOptions DefaultLangOpts;
630
631 // FIXME: Need to set -std to get all the implicit options.
632
633 // FIXME: We want to only pass options relative to the defaults, which
634 // requires constructing a target. :(
635 //
636 // It would be better to push the all target specific choices into the driver,
637 // so that everything below that was more uniform.
638
639 if (Opts.Trigraphs)
640 Res.push_back("-trigraphs");
641 // Implicit based on the input kind:
642 // AsmPreprocessor, CPlusPlus, ObjC1, ObjC2, OpenCL
643 // Implicit based on the input language standard:
644 // BCPLComment, C99, CPlusPlus0x, Digraphs, GNUInline, ImplicitInt, GNUMode
645 if (Opts.DollarIdents)
646 Res.push_back("-fdollars-in-identifiers");
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000647 if (Opts.GNUMode && !Opts.GNUKeywords)
648 Res.push_back("-fno-gnu-keywords");
649 if (!Opts.GNUMode && Opts.GNUKeywords)
650 Res.push_back("-fgnu-keywords");
Francois Pichet62ec1f22011-09-17 17:15:52 +0000651 if (Opts.MicrosoftExt)
Daniel Dunbar8663b182009-12-16 20:10:18 +0000652 Res.push_back("-fms-extensions");
Douglas Gregor0bb935c2011-10-24 15:27:23 +0000653 if (Opts.MicrosoftMode)
654 Res.push_back("-fms-compatibility");
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000655 if (Opts.MSCVersion != 0)
656 Res.push_back("-fmsc-version=" + llvm::utostr(Opts.MSCVersion));
Dawn Perchik400b6072010-09-02 23:59:25 +0000657 if (Opts.Borland)
658 Res.push_back("-fborland-extensions");
John McCalld1e40d52011-10-02 01:16:38 +0000659 if (!Opts.ObjCNonFragileABI)
660 Res.push_back("-fobjc-fragile-abi");
Ted Kremenekc32647d2010-12-23 21:35:43 +0000661 if (Opts.ObjCDefaultSynthProperties)
662 Res.push_back("-fobjc-default-synthesize-properties");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000663 // NoInline is implicit.
664 if (!Opts.CXXOperatorNames)
665 Res.push_back("-fno-operator-names");
666 if (Opts.PascalStrings)
667 Res.push_back("-fpascal-strings");
Mike Stump9c276ae2009-12-12 01:27:46 +0000668 if (Opts.CatchUndefined)
669 Res.push_back("-fcatch-undefined-behavior");
Kostya Serebryanyb6196882011-11-22 01:28:36 +0000670 if (Opts.AddressSanitizer)
671 Res.push_back("-faddress-sanitizer");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000672 if (Opts.WritableStrings)
673 Res.push_back("-fwritable-strings");
John McCall4b7a8342010-03-15 10:54:44 +0000674 if (Opts.ConstStrings)
Chandler Carruth50465d12011-04-23 06:30:43 +0000675 Res.push_back("-fconst-strings");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000676 if (!Opts.LaxVectorConversions)
677 Res.push_back("-fno-lax-vector-conversions");
678 if (Opts.AltiVec)
679 Res.push_back("-faltivec");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000680 if (Opts.Exceptions)
681 Res.push_back("-fexceptions");
Anders Carlsson0761e442011-02-23 03:16:42 +0000682 if (Opts.ObjCExceptions)
683 Res.push_back("-fobjc-exceptions");
684 if (Opts.CXXExceptions)
685 Res.push_back("-fcxx-exceptions");
Daniel Dunbar73482882010-02-10 18:48:44 +0000686 if (Opts.SjLjExceptions)
687 Res.push_back("-fsjlj-exceptions");
Daniel Dunbar2ed42282011-03-18 21:23:38 +0000688 if (Opts.TraditionalCPP)
689 Res.push_back("-traditional-cpp");
Mike Stumpde050572009-12-02 18:57:08 +0000690 if (!Opts.RTTI)
Daniel Dunbar4db166b2009-11-19 05:32:09 +0000691 Res.push_back("-fno-rtti");
Douglas Gregor6f755502011-02-01 15:15:22 +0000692 if (Opts.MSBitfields)
693 Res.push_back("-mms-bitfields");
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +0000694 if (!Opts.NeXTRuntime)
695 Res.push_back("-fgnu-runtime");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000696 if (Opts.Freestanding)
697 Res.push_back("-ffreestanding");
698 if (Opts.NoBuiltin)
699 Res.push_back("-fno-builtin");
Nuno Lopesfc284482009-12-16 16:59:22 +0000700 if (!Opts.AssumeSaneOperatorNew)
701 Res.push_back("-fno-assume-sane-operator-new");
Anders Carlssona508b7d2010-02-06 23:23:06 +0000702 if (!Opts.ThreadsafeStatics)
703 Res.push_back("-fno-threadsafe-statics");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000704 if (Opts.POSIXThreads)
705 Res.push_back("-pthread");
Daniel Dunbara51ca522009-11-17 09:15:57 +0000706 if (Opts.Blocks)
Daniel Dunbare50c1672009-11-29 05:52:21 +0000707 Res.push_back("-fblocks");
John McCall13db5cf2011-09-09 20:41:01 +0000708 if (Opts.BlocksRuntimeOptional)
709 Res.push_back("-fblocks-runtime-optional");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000710 if (Opts.EmitAllDecls)
711 Res.push_back("-femit-all-decls");
Dan Gohmanc31176d2010-01-08 02:20:44 +0000712 if (Opts.MathErrno)
713 Res.push_back("-fmath-errno");
Chris Lattnera4d71452010-06-26 21:25:03 +0000714 switch (Opts.getSignedOverflowBehavior()) {
715 case LangOptions::SOB_Undefined: break;
716 case LangOptions::SOB_Defined: Res.push_back("-fwrapv"); break;
Michael J. Spencer20249a12010-10-21 03:16:25 +0000717 case LangOptions::SOB_Trapping:
Douglas Gregor21a25162011-11-02 20:52:01 +0000718 Res.push_back("-ftrapv");
David Chisnall7f18e672010-09-17 18:29:54 +0000719 if (!Opts.OverflowHandler.empty()) {
720 Res.push_back("-ftrapv-handler");
721 Res.push_back(Opts.OverflowHandler);
722 }
Douglas Gregor21a25162011-11-02 20:52:01 +0000723 break;
Chris Lattnera4d71452010-06-26 21:25:03 +0000724 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000725 if (Opts.HeinousExtensions)
726 Res.push_back("-fheinous-gnu-extensions");
727 // Optimize is implicit.
728 // OptimizeSize is implicit.
729 if (Opts.Static)
730 Res.push_back("-static-define");
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000731 if (Opts.DumpRecordLayouts)
732 Res.push_back("-fdump-record-layouts");
Anders Carlsson046c2942010-04-17 20:15:18 +0000733 if (Opts.DumpVTableLayouts)
Daniel Dunbarfb937b82010-04-15 15:06:22 +0000734 Res.push_back("-fdump-vtable-layouts");
735 if (Opts.NoBitFieldTypeAlign)
736 Res.push_back("-fno-bitfield-type-alignment");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000737 if (Opts.PICLevel) {
738 Res.push_back("-pic-level");
739 Res.push_back(llvm::utostr(Opts.PICLevel));
740 }
741 if (Opts.ObjCGCBitmapPrint)
742 Res.push_back("-print-ivar-layout");
Fariborz Jahanian33e982b2010-04-22 20:26:39 +0000743 if (Opts.NoConstantCFStrings)
744 Res.push_back("-fno-constant-cfstrings");
John McCall7002f4c2010-04-09 19:03:51 +0000745 if (!Opts.AccessControl)
746 Res.push_back("-fno-access-control");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000747 if (!Opts.CharIsSigned)
Daniel Dunbare50c1672009-11-29 05:52:21 +0000748 Res.push_back("-fno-signed-char");
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000749 if (Opts.ShortWChar)
750 Res.push_back("-fshort-wchar");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000751 if (!Opts.ElideConstructors)
752 Res.push_back("-fno-elide-constructors");
Douglas Gregore289d812011-09-13 17:21:33 +0000753 if (Opts.getGC() != LangOptions::NonGC) {
754 if (Opts.getGC() == LangOptions::HybridGC) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000755 Res.push_back("-fobjc-gc");
756 } else {
Douglas Gregore289d812011-09-13 17:21:33 +0000757 assert(Opts.getGC() == LangOptions::GCOnly && "Invalid GC mode!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000758 Res.push_back("-fobjc-gc-only");
759 }
760 }
John McCallf85e1932011-06-15 23:02:42 +0000761 if (Opts.ObjCAutoRefCount)
762 Res.push_back("-fobjc-arc");
John McCall9f084a32011-07-06 00:26:06 +0000763 if (Opts.ObjCRuntimeHasWeak)
764 Res.push_back("-fobjc-runtime-has-weak");
Douglas Gregor74da19f2011-06-14 23:20:43 +0000765 if (!Opts.ObjCInferRelatedResultType)
766 Res.push_back("-fno-objc-infer-related-result-type");
767
Fariborz Jahanianb466d012011-01-07 01:05:02 +0000768 if (Opts.AppleKext)
769 Res.push_back("-fapple-kext");
770
John McCall1fb0caa2010-10-22 21:05:15 +0000771 if (Opts.getVisibilityMode() != DefaultVisibility) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000772 Res.push_back("-fvisibility");
John McCall1fb0caa2010-10-22 21:05:15 +0000773 if (Opts.getVisibilityMode() == HiddenVisibility) {
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +0000774 Res.push_back("hidden");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000775 } else {
John McCall1fb0caa2010-10-22 21:05:15 +0000776 assert(Opts.getVisibilityMode() == ProtectedVisibility &&
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000777 "Invalid visibility!");
778 Res.push_back("protected");
779 }
780 }
Douglas Gregor7cf84d62010-06-15 17:05:35 +0000781 if (Opts.InlineVisibilityHidden)
782 Res.push_back("-fvisibility-inlines-hidden");
Michael J. Spencer20249a12010-10-21 03:16:25 +0000783
Douglas Gregore289d812011-09-13 17:21:33 +0000784 if (Opts.getStackProtector() != 0) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000785 Res.push_back("-stack-protector");
Douglas Gregore289d812011-09-13 17:21:33 +0000786 Res.push_back(llvm::utostr(Opts.getStackProtector()));
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000787 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000788 if (Opts.InstantiationDepth != DefaultLangOpts.InstantiationDepth) {
789 Res.push_back("-ftemplate-depth");
790 Res.push_back(llvm::utostr(Opts.InstantiationDepth));
791 }
Richard Smithc18c4232011-11-21 19:36:32 +0000792 if (Opts.ConstexprCallDepth != DefaultLangOpts.ConstexprCallDepth) {
793 Res.push_back("-fconstexpr-depth");
794 Res.push_back(llvm::utostr(Opts.ConstexprCallDepth));
795 }
Daniel Dunbar1b096952009-11-29 02:38:47 +0000796 if (!Opts.ObjCConstantStringClass.empty()) {
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000797 Res.push_back("-fconstant-string-class");
798 Res.push_back(Opts.ObjCConstantStringClass);
799 }
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000800 if (Opts.FakeAddressSpaceMap)
801 Res.push_back("-ffake-address-space-map");
John McCalla5fc4722011-04-09 22:50:59 +0000802 if (Opts.ParseUnknownAnytype)
803 Res.push_back("-funknown-anytype");
John McCall48218c62011-07-13 17:56:40 +0000804 if (Opts.DebuggerSupport)
805 Res.push_back("-fdebugger-support");
Francois Pichet8387e2a2011-04-22 22:18:13 +0000806 if (Opts.DelayedTemplateParsing)
807 Res.push_back("-fdelayed-template-parsing");
Chandler Carruthf8c247d2011-04-23 19:48:40 +0000808 if (Opts.Deprecated)
809 Res.push_back("-fdeprecated-macro");
Eli Friedman19bda3a2011-11-02 01:53:16 +0000810 if (Opts.ApplePragmaPack)
811 Res.push_back("-fapple-pragma-pack");
Douglas Gregorb86b8dc2011-11-15 19:35:01 +0000812 if (!Opts.CurrentModule.empty())
813 Res.push_back("-fmodule-name=" + Opts.CurrentModule);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000814}
815
816static void PreprocessorOptsToArgs(const PreprocessorOptions &Opts,
817 std::vector<std::string> &Res) {
818 for (unsigned i = 0, e = Opts.Macros.size(); i != e; ++i)
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000819 Res.push_back(std::string(Opts.Macros[i].second ? "-U" : "-D") +
820 Opts.Macros[i].first);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000821 for (unsigned i = 0, e = Opts.Includes.size(); i != e; ++i) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000822 // FIXME: We need to avoid reincluding the implicit PCH and PTH includes.
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000823 Res.push_back("-include");
824 Res.push_back(Opts.Includes[i]);
825 }
826 for (unsigned i = 0, e = Opts.MacroIncludes.size(); i != e; ++i) {
827 Res.push_back("-imacros");
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000828 Res.push_back(Opts.MacroIncludes[i]);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000829 }
830 if (!Opts.UsePredefines)
831 Res.push_back("-undef");
Douglas Gregor94dc8f62010-03-19 16:15:56 +0000832 if (Opts.DetailedRecord)
833 Res.push_back("-detailed-preprocessing-record");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000834 if (!Opts.ImplicitPCHInclude.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000835 Res.push_back("-include-pch");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000836 Res.push_back(Opts.ImplicitPCHInclude);
837 }
838 if (!Opts.ImplicitPTHInclude.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000839 Res.push_back("-include-pth");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000840 Res.push_back(Opts.ImplicitPTHInclude);
841 }
842 if (!Opts.TokenCache.empty()) {
Daniel Dunbar0ff679f2009-11-26 02:14:07 +0000843 if (Opts.ImplicitPTHInclude.empty()) {
844 Res.push_back("-token-cache");
845 Res.push_back(Opts.TokenCache);
846 } else
847 assert(Opts.ImplicitPTHInclude == Opts.TokenCache &&
848 "Unsupported option combination!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000849 }
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000850 for (unsigned i = 0, e = Opts.ChainedIncludes.size(); i != e; ++i) {
851 Res.push_back("-chain-include");
852 Res.push_back(Opts.ChainedIncludes[i]);
853 }
Daniel Dunbare14b5f52009-12-03 05:11:16 +0000854 for (unsigned i = 0, e = Opts.RemappedFiles.size(); i != e; ++i) {
855 Res.push_back("-remap-file");
856 Res.push_back(Opts.RemappedFiles[i].first + ";" +
857 Opts.RemappedFiles[i].second);
858 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000859}
860
861static void PreprocessorOutputOptsToArgs(const PreprocessorOutputOptions &Opts,
862 std::vector<std::string> &Res) {
863 if (!Opts.ShowCPP && !Opts.ShowMacros)
Chris Lattner83e7a782010-04-07 22:58:06 +0000864 llvm::report_fatal_error("Invalid option combination!");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000865
866 if (Opts.ShowCPP && Opts.ShowMacros)
867 Res.push_back("-dD");
868 else if (!Opts.ShowCPP && Opts.ShowMacros)
869 Res.push_back("-dM");
870
871 if (!Opts.ShowLineMarkers)
872 Res.push_back("-P");
873 if (Opts.ShowComments)
874 Res.push_back("-C");
875 if (Opts.ShowMacroComments)
876 Res.push_back("-CC");
877}
878
879static void TargetOptsToArgs(const TargetOptions &Opts,
880 std::vector<std::string> &Res) {
881 Res.push_back("-triple");
882 Res.push_back(Opts.Triple);
883 if (!Opts.CPU.empty()) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000884 Res.push_back("-target-cpu");
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000885 Res.push_back(Opts.CPU);
886 }
887 if (!Opts.ABI.empty()) {
888 Res.push_back("-target-abi");
889 Res.push_back(Opts.ABI);
890 }
Daniel Dunbar39065a52010-08-11 23:07:42 +0000891 if (!Opts.LinkerVersion.empty()) {
892 Res.push_back("-target-linker-version");
893 Res.push_back(Opts.LinkerVersion);
894 }
John McCall875ab102010-08-22 06:43:33 +0000895 if (!Opts.CXXABI.empty()) {
896 Res.push_back("-cxx-abi");
897 Res.push_back(Opts.CXXABI);
898 }
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000899 for (unsigned i = 0, e = Opts.Features.size(); i != e; ++i) {
900 Res.push_back("-target-feature");
901 Res.push_back(Opts.Features[i]);
902 }
903}
904
905void CompilerInvocation::toArgs(std::vector<std::string> &Res) {
906 AnalyzerOptsToArgs(getAnalyzerOpts(), Res);
907 CodeGenOptsToArgs(getCodeGenOpts(), Res);
908 DependencyOutputOptsToArgs(getDependencyOutputOpts(), Res);
909 DiagnosticOptsToArgs(getDiagnosticOpts(), Res);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000910 FileSystemOptsToArgs(getFileSystemOpts(), Res);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000911 FrontendOptsToArgs(getFrontendOpts(), Res);
912 HeaderSearchOptsToArgs(getHeaderSearchOpts(), Res);
Ted Kremenekd3b74d92011-11-17 23:01:24 +0000913 LangOptsToArgs(*getLangOpts(), Res);
Daniel Dunbarb3375cb2009-11-17 06:02:29 +0000914 PreprocessorOptsToArgs(getPreprocessorOpts(), Res);
915 PreprocessorOutputOptsToArgs(getPreprocessorOutputOpts(), Res);
916 TargetOptsToArgs(getTargetOpts(), Res);
917}
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000918
919//===----------------------------------------------------------------------===//
920// Deserialization (to args)
921//===----------------------------------------------------------------------===//
922
923using namespace clang::driver;
924using namespace clang::driver::cc1options;
925
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000926//
927
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +0000928static unsigned getOptimizationLevel(ArgList &Args, InputKind IK,
David Blaikied6471f72011-09-25 23:23:43 +0000929 DiagnosticsEngine &Diags) {
Peter Collingbourne61e07862010-12-04 01:50:45 +0000930 unsigned DefaultOpt = 0;
931 if (IK == IK_OpenCL && !Args.hasArg(OPT_cl_opt_disable))
932 DefaultOpt = 2;
Bob Wilsona0fa2032011-04-29 22:49:50 +0000933 // -Os/-Oz implies -O2
934 return (Args.hasArg(OPT_Os) || Args.hasArg (OPT_Oz)) ? 2 :
Peter Collingbourne61e07862010-12-04 01:50:45 +0000935 Args.getLastArgIntValue(OPT_O, DefaultOpt, Diags);
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +0000936}
937
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000938static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args,
David Blaikied6471f72011-09-25 23:23:43 +0000939 DiagnosticsEngine &Diags) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000940 using namespace cc1options;
941
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000942 if (Arg *A = Args.getLastArg(OPT_analyzer_store)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000943 StringRef Name = A->getValue(Args);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000944 AnalysisStores Value = llvm::StringSwitch<AnalysisStores>(Name)
945#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) \
946 .Case(CMDFLAG, NAME##Model)
947#include "clang/Frontend/Analyses.def"
948 .Default(NumStores);
949 // FIXME: Error handling.
950 if (Value == NumStores)
951 Diags.Report(diag::err_drv_invalid_value)
Daniel Dunbarbfbb39d2010-06-09 22:30:54 +0000952 << A->getAsString(Args) << Name;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000953 else
954 Opts.AnalysisStoreOpt = Value;
955 }
956
957 if (Arg *A = Args.getLastArg(OPT_analyzer_constraints)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000958 StringRef Name = A->getValue(Args);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000959 AnalysisConstraints Value = llvm::StringSwitch<AnalysisConstraints>(Name)
960#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) \
961 .Case(CMDFLAG, NAME##Model)
962#include "clang/Frontend/Analyses.def"
963 .Default(NumConstraints);
964 // FIXME: Error handling.
965 if (Value == NumConstraints)
966 Diags.Report(diag::err_drv_invalid_value)
Daniel Dunbarbfbb39d2010-06-09 22:30:54 +0000967 << A->getAsString(Args) << Name;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000968 else
969 Opts.AnalysisConstraintsOpt = Value;
970 }
971
972 if (Arg *A = Args.getLastArg(OPT_analyzer_output)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000973 StringRef Name = A->getValue(Args);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000974 AnalysisDiagClients Value = llvm::StringSwitch<AnalysisDiagClients>(Name)
975#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREAT) \
976 .Case(CMDFLAG, PD_##NAME)
977#include "clang/Frontend/Analyses.def"
978 .Default(NUM_ANALYSIS_DIAG_CLIENTS);
979 // FIXME: Error handling.
980 if (Value == NUM_ANALYSIS_DIAG_CLIENTS)
981 Diags.Report(diag::err_drv_invalid_value)
Daniel Dunbarbfbb39d2010-06-09 22:30:54 +0000982 << A->getAsString(Args) << Name;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000983 else
984 Opts.AnalysisDiagOpt = Value;
985 }
986
Anna Zaksd3095282011-09-30 02:03:00 +0000987 if (Arg *A = Args.getLastArg(OPT_analyzer_purge)) {
988 StringRef Name = A->getValue(Args);
989 AnalysisPurgeMode Value = llvm::StringSwitch<AnalysisPurgeMode>(Name)
990#define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) \
991 .Case(CMDFLAG, NAME)
992#include "clang/Frontend/Analyses.def"
993 .Default(NumPurgeModes);
994 // FIXME: Error handling.
995 if (Value == NumPurgeModes)
996 Diags.Report(diag::err_drv_invalid_value)
997 << A->getAsString(Args) << Name;
998 else
999 Opts.AnalysisPurgeOpt = Value;
1000 }
1001
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +00001002 Opts.ShowCheckerHelp = Args.hasArg(OPT_analyzer_checker_help);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001003 Opts.VisualizeEGDot = Args.hasArg(OPT_analyzer_viz_egraph_graphviz);
1004 Opts.VisualizeEGUbi = Args.hasArg(OPT_analyzer_viz_egraph_ubigraph);
1005 Opts.AnalyzeAll = Args.hasArg(OPT_analyzer_opt_analyze_headers);
1006 Opts.AnalyzerDisplayProgress = Args.hasArg(OPT_analyzer_display_progress);
Ted Kremenekfc576512009-12-07 22:06:12 +00001007 Opts.AnalyzeNestedBlocks =
1008 Args.hasArg(OPT_analyzer_opt_analyze_nested_blocks);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001009 Opts.EagerlyAssume = Args.hasArg(OPT_analyzer_eagerly_assume);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001010 Opts.AnalyzeSpecificFunction = Args.getLastArgValue(OPT_analyze_function);
Ted Kremenek9b823e82010-08-03 00:09:51 +00001011 Opts.UnoptimizedCFG = Args.hasArg(OPT_analysis_UnoptimizedCFG);
Marcin Swiderski9121ba22010-09-30 07:41:24 +00001012 Opts.CFGAddImplicitDtors = Args.hasArg(OPT_analysis_CFGAddImplicitDtors);
1013 Opts.CFGAddInitializers = Args.hasArg(OPT_analysis_CFGAddInitializers);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001014 Opts.TrimGraph = Args.hasArg(OPT_trim_egraph);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001015 Opts.MaxNodes = Args.getLastArgIntValue(OPT_analyzer_max_nodes, 150000,Diags);
Ted Kremenek6648e5d2010-10-18 23:36:05 +00001016 Opts.MaxLoop = Args.getLastArgIntValue(OPT_analyzer_max_loop, 4, Diags);
Ted Kremenekd767d812011-02-09 01:27:33 +00001017 Opts.EagerlyTrimEGraph = !Args.hasArg(OPT_analyzer_no_eagerly_trim_egraph);
Zhongxing Xu7b99d122010-05-06 02:59:29 +00001018 Opts.InlineCall = Args.hasArg(OPT_analyzer_inline_call);
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +00001019
1020 Opts.CheckersControlList.clear();
1021 for (arg_iterator it = Args.filtered_begin(OPT_analyzer_checker,
1022 OPT_analyzer_disable_checker),
1023 ie = Args.filtered_end(); it != ie; ++it) {
1024 const Arg *A = *it;
1025 A->claim();
1026 bool enable = (A->getOption().getID() == OPT_analyzer_checker);
Argyrios Kyrtzidis38c8fe72011-02-24 08:42:20 +00001027 // We can have a list of comma separated checker names, e.g:
1028 // '-analyzer-checker=cocoa,unix'
Chris Lattner5f9e2722011-07-23 10:55:15 +00001029 StringRef checkerList = A->getValue(Args);
1030 SmallVector<StringRef, 4> checkers;
Argyrios Kyrtzidis38c8fe72011-02-24 08:42:20 +00001031 checkerList.split(checkers, ",");
1032 for (unsigned i = 0, e = checkers.size(); i != e; ++i)
1033 Opts.CheckersControlList.push_back(std::make_pair(checkers[i], enable));
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +00001034 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001035}
1036
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +00001037static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
David Blaikied6471f72011-09-25 23:23:43 +00001038 DiagnosticsEngine &Diags) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001039 using namespace cc1options;
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +00001040
1041 Opts.OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
1042 if (Opts.OptimizationLevel > 3) {
1043 Diags.Report(diag::err_drv_invalid_value)
1044 << Args.getLastArg(OPT_O)->getAsString(Args) << Opts.OptimizationLevel;
1045 Opts.OptimizationLevel = 3;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001046 }
1047
1048 // We must always run at least the always inlining pass.
1049 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
1050 : CodeGenOptions::OnlyAlwaysInlining;
1051
1052 Opts.DebugInfo = Args.hasArg(OPT_g);
Devang Patel033be8b2011-11-04 20:05:58 +00001053 Opts.LimitDebugInfo = !Args.hasArg(OPT_fno_limit_debug_info)
1054 || Args.hasArg(OPT_flimit_debug_info);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001055 Opts.DisableLLVMOpts = Args.hasArg(OPT_disable_llvm_optzns);
1056 Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
John McCall32096692011-03-18 02:56:14 +00001057 Opts.ForbidGuardVariables = Args.hasArg(OPT_fforbid_guard_variables);
Daniel Dunbare26bdb92011-06-21 18:54:46 +00001058 Opts.UseRegisterSizedBitfieldAccess = Args.hasArg(
1059 OPT_fuse_register_sized_bitfield_access);
Dan Gohman4d5625e2010-10-14 22:36:56 +00001060 Opts.RelaxedAliasing = Args.hasArg(OPT_relaxed_aliasing);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001061 Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001062 Opts.MergeAllConstants = !Args.hasArg(OPT_fno_merge_all_constants);
1063 Opts.NoCommon = Args.hasArg(OPT_fno_common);
1064 Opts.NoImplicitFloat = Args.hasArg(OPT_no_implicit_float);
1065 Opts.OptimizeSize = Args.hasArg(OPT_Os);
Bob Wilsona0fa2032011-04-29 22:49:50 +00001066 Opts.OptimizeSize = Args.hasArg(OPT_Oz) ? 2 : Opts.OptimizeSize;
Daniel Dunbar6b0cf672010-06-07 23:19:17 +00001067 Opts.SimplifyLibCalls = !(Args.hasArg(OPT_fno_builtin) ||
1068 Args.hasArg(OPT_ffreestanding));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001069 Opts.UnrollLoops = Args.hasArg(OPT_funroll_loops) ||
Eric Christopherf84d4092010-08-07 23:08:14 +00001070 (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001071
1072 Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose);
John McCallf85e1932011-06-15 23:02:42 +00001073 Opts.ObjCAutoRefCountExceptions = Args.hasArg(OPT_fobjc_arc_exceptions);
John McCall9f084a32011-07-06 00:26:06 +00001074 Opts.ObjCRuntimeHasARC = Args.hasArg(OPT_fobjc_runtime_has_arc);
John McCall256a76e2011-07-06 01:22:26 +00001075 Opts.ObjCRuntimeHasTerminate = Args.hasArg(OPT_fobjc_runtime_has_terminate);
Peter Collingbourned51e43a2011-10-06 18:29:46 +00001076 Opts.CUDAIsDevice = Args.hasArg(OPT_fcuda_is_device);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001077 Opts.CXAAtExit = !Args.hasArg(OPT_fno_use_cxa_atexit);
1078 Opts.CXXCtorDtorAliases = Args.hasArg(OPT_mconstructor_aliases);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001079 Opts.CodeModel = Args.getLastArgValue(OPT_mcode_model);
1080 Opts.DebugPass = Args.getLastArgValue(OPT_mdebug_pass);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001081 Opts.DisableFPElim = Args.hasArg(OPT_mdisable_fp_elim);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001082 Opts.FloatABI = Args.getLastArgValue(OPT_mfloat_abi);
John McCall279b5eb2010-08-12 23:36:15 +00001083 Opts.HiddenWeakVTables = Args.hasArg(OPT_fhidden_weak_vtables);
Peter Collingbourne4400cb82010-12-04 01:51:33 +00001084 Opts.LessPreciseFPMAD = Args.hasArg(OPT_cl_mad_enable);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001085 Opts.LimitFloatPrecision = Args.getLastArgValue(OPT_mlimit_float_precision);
Peter Collingbournef0840822010-12-04 01:51:23 +00001086 Opts.NoInfsFPMath = Opts.NoNaNsFPMath = Args.hasArg(OPT_cl_finite_math_only)||
1087 Args.hasArg(OPT_cl_fast_relaxed_math);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001088 Opts.NoZeroInitializedInBSS = Args.hasArg(OPT_mno_zero_initialized_in_bss);
Daniel Dunbar3c66d302011-03-22 16:48:17 +00001089 Opts.BackendOptions = Args.getAllArgValues(OPT_backend_option);
Daniel Dunbar17d3fea2011-02-09 17:54:19 +00001090 Opts.NumRegisterParameters = Args.getLastArgIntValue(OPT_mregparm, 0, Diags);
Chad Rosier1b906052011-08-26 00:26:29 +00001091 Opts.NoGlobalMerge = Args.hasArg(OPT_mno_global_merge);
Nick Lewyckyc3b90142011-06-21 00:14:18 +00001092 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
Daniel Dunbar4ecc9b72010-05-27 05:39:39 +00001093 Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
Daniel Dunbar1ad66482010-07-01 01:31:45 +00001094 Opts.OmitLeafFramePointer = Args.hasArg(OPT_momit_leaf_frame_pointer);
Daniel Dunbar96932322011-03-28 22:49:28 +00001095 Opts.SaveTempLabels = Args.hasArg(OPT_msave_temp_labels);
Rafael Espindolaf24a1512011-04-30 18:35:43 +00001096 Opts.NoDwarf2CFIAsm = Args.hasArg(OPT_fno_dwarf2_cfi_asm);
Nick Lewyckyea523d72011-10-17 23:05:52 +00001097 Opts.NoDwarfDirectoryAsm = Args.hasArg(OPT_fno_dwarf_directory_asm);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001098 Opts.SoftFloat = Args.hasArg(OPT_msoft_float);
Peter Collingbournef0840822010-12-04 01:51:23 +00001099 Opts.UnsafeFPMath = Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
1100 Args.hasArg(OPT_cl_fast_relaxed_math);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001101 Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001102 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001103
Chris Lattnerbbea7162010-04-13 00:38:24 +00001104 Opts.FunctionSections = Args.hasArg(OPT_ffunction_sections);
1105 Opts.DataSections = Args.hasArg(OPT_fdata_sections);
1106
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001107 Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name);
John McCall824e19e2010-02-12 23:47:27 +00001108 Opts.VerifyModule = !Args.hasArg(OPT_disable_llvm_verifier);
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001109
Chris Lattner7255a2d2010-06-22 00:03:40 +00001110 Opts.InstrumentFunctions = Args.hasArg(OPT_finstrument_functions);
Roman Divackybe4c8702011-02-10 16:52:03 +00001111 Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
Nick Lewyckye8ba8d72011-04-21 23:44:07 +00001112 Opts.EmitGcovArcs = Args.hasArg(OPT_femit_coverage_data);
1113 Opts.EmitGcovNotes = Args.hasArg(OPT_femit_coverage_notes);
Nick Lewycky3dc05412011-05-05 00:08:20 +00001114 Opts.CoverageFile = Args.getLastArgValue(OPT_coverage_file);
Nick Lewycky7c4fd912011-10-21 02:32:14 +00001115 Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +00001116 Opts.LinkBitcodeFile = Args.getLastArgValue(OPT_mlink_bitcode_file);
Chris Lattner7255a2d2010-06-22 00:03:40 +00001117
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001118 if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001119 StringRef Name = A->getValue(Args);
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001120 unsigned Method = llvm::StringSwitch<unsigned>(Name)
1121 .Case("legacy", CodeGenOptions::Legacy)
1122 .Case("non-legacy", CodeGenOptions::NonLegacy)
1123 .Case("mixed", CodeGenOptions::Mixed)
1124 .Default(~0U);
1125 if (Method == ~0U)
1126 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
1127 else
1128 Opts.ObjCDispatchMethod = Method;
1129 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001130}
1131
1132static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
1133 ArgList &Args) {
1134 using namespace cc1options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001135 Opts.OutputFile = Args.getLastArgValue(OPT_dependency_file);
1136 Opts.Targets = Args.getAllArgValues(OPT_MT);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001137 Opts.IncludeSystemHeaders = Args.hasArg(OPT_sys_header_deps);
1138 Opts.UsePhonyTargets = Args.hasArg(OPT_MP);
Daniel Dunbareef63e02011-02-02 15:41:17 +00001139 Opts.ShowHeaderIncludes = Args.hasArg(OPT_H);
Daniel Dunbarb34d69b2011-02-02 21:11:31 +00001140 Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file);
Peter Collingbournebb527862011-07-12 19:35:15 +00001141 Opts.AddMissingHeaderDeps = Args.hasArg(OPT_MG);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001142}
1143
1144static void ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
David Blaikied6471f72011-09-25 23:23:43 +00001145 DiagnosticsEngine &Diags) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001146 using namespace cc1options;
Daniel Dunbar08c66952011-04-07 18:11:14 +00001147 Opts.DiagnosticLogFile = Args.getLastArgValue(OPT_diagnostic_log_file);
Ted Kremenek78002122011-10-29 00:12:39 +00001148 Opts.DiagnosticSerializationFile =
1149 Args.getLastArgValue(OPT_diagnostic_serialized_file);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001150 Opts.IgnoreWarnings = Args.hasArg(OPT_w);
1151 Opts.NoRewriteMacros = Args.hasArg(OPT_Wno_rewrite_macros);
1152 Opts.Pedantic = Args.hasArg(OPT_pedantic);
1153 Opts.PedanticErrors = Args.hasArg(OPT_pedantic_errors);
1154 Opts.ShowCarets = !Args.hasArg(OPT_fno_caret_diagnostics);
1155 Opts.ShowColors = Args.hasArg(OPT_fcolor_diagnostics);
Douglas Gregorc9471b02011-05-21 17:07:29 +00001156 Opts.ShowColumn = Args.hasFlag(OPT_fshow_column,
1157 OPT_fno_show_column,
1158 /*Default=*/true);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001159 Opts.ShowFixits = !Args.hasArg(OPT_fno_diagnostics_fixit_info);
1160 Opts.ShowLocation = !Args.hasArg(OPT_fno_show_source_location);
Douglas Gregor7d2b8c12011-04-15 22:04:17 +00001161 Opts.ShowNames = Args.hasArg(OPT_fdiagnostics_show_name);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001162 Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option);
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001163
Chandler Carruth34b8e682011-03-27 20:00:08 +00001164 // Default behavior is to not to show note include stacks.
1165 Opts.ShowNoteIncludeStack = false;
Chandler Carruthabaca7a2011-03-27 01:50:55 +00001166 if (Arg *A = Args.getLastArg(OPT_fdiagnostics_show_note_include_stack,
1167 OPT_fno_diagnostics_show_note_include_stack))
Chandler Carruth34b8e682011-03-27 20:00:08 +00001168 if (A->getOption().matches(OPT_fdiagnostics_show_note_include_stack))
1169 Opts.ShowNoteIncludeStack = true;
Chandler Carruthabaca7a2011-03-27 01:50:55 +00001170
Chris Lattner5f9e2722011-07-23 10:55:15 +00001171 StringRef ShowOverloads =
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001172 Args.getLastArgValue(OPT_fshow_overloads_EQ, "all");
1173 if (ShowOverloads == "best")
David Blaikied6471f72011-09-25 23:23:43 +00001174 Opts.ShowOverloads = DiagnosticsEngine::Ovl_Best;
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001175 else if (ShowOverloads == "all")
David Blaikied6471f72011-09-25 23:23:43 +00001176 Opts.ShowOverloads = DiagnosticsEngine::Ovl_All;
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001177 else
1178 Diags.Report(diag::err_drv_invalid_value)
1179 << Args.getLastArg(OPT_fshow_overloads_EQ)->getAsString(Args)
1180 << ShowOverloads;
1181
Chris Lattner5f9e2722011-07-23 10:55:15 +00001182 StringRef ShowCategory =
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001183 Args.getLastArgValue(OPT_fdiagnostics_show_category, "none");
Chris Lattner6fbe8392010-05-04 21:55:25 +00001184 if (ShowCategory == "none")
1185 Opts.ShowCategories = 0;
1186 else if (ShowCategory == "id")
1187 Opts.ShowCategories = 1;
1188 else if (ShowCategory == "name")
1189 Opts.ShowCategories = 2;
1190 else
1191 Diags.Report(diag::err_drv_invalid_value)
1192 << Args.getLastArg(OPT_fdiagnostics_show_category)->getAsString(Args)
1193 << ShowCategory;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001194
Chris Lattner5f9e2722011-07-23 10:55:15 +00001195 StringRef Format =
Douglas Gregorc9471b02011-05-21 17:07:29 +00001196 Args.getLastArgValue(OPT_fdiagnostics_format, "clang");
1197 if (Format == "clang")
1198 Opts.Format = DiagnosticOptions::Clang;
1199 else if (Format == "msvc")
1200 Opts.Format = DiagnosticOptions::Msvc;
1201 else if (Format == "vi")
1202 Opts.Format = DiagnosticOptions::Vi;
1203 else
1204 Diags.Report(diag::err_drv_invalid_value)
1205 << Args.getLastArg(OPT_fdiagnostics_format)->getAsString(Args)
1206 << Format;
1207
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001208 Opts.ShowSourceRanges = Args.hasArg(OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +00001209 Opts.ShowParseableFixits = Args.hasArg(OPT_fdiagnostics_parseable_fixits);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001210 Opts.VerifyDiagnostics = Args.hasArg(OPT_verify);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001211 Opts.ErrorLimit = Args.getLastArgIntValue(OPT_ferror_limit, 0, Diags);
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001212 Opts.MacroBacktraceLimit
Michael J. Spencer20249a12010-10-21 03:16:25 +00001213 = Args.getLastArgIntValue(OPT_fmacro_backtrace_limit,
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001214 DiagnosticOptions::DefaultMacroBacktraceLimit, Diags);
Douglas Gregor575cf372010-04-20 07:18:24 +00001215 Opts.TemplateBacktraceLimit
Michael J. Spencer20249a12010-10-21 03:16:25 +00001216 = Args.getLastArgIntValue(OPT_ftemplate_backtrace_limit,
1217 DiagnosticOptions::DefaultTemplateBacktraceLimit,
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001218 Diags);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001219 Opts.TabStop = Args.getLastArgIntValue(OPT_ftabstop,
Chris Lattner52388f92010-01-13 03:06:50 +00001220 DiagnosticOptions::DefaultTabStop, Diags);
1221 if (Opts.TabStop == 0 || Opts.TabStop > DiagnosticOptions::MaxTabStop) {
1222 Diags.Report(diag::warn_ignoring_ftabstop_value)
1223 << Opts.TabStop << DiagnosticOptions::DefaultTabStop;
1224 Opts.TabStop = DiagnosticOptions::DefaultTabStop;
1225 }
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001226 Opts.MessageLength = Args.getLastArgIntValue(OPT_fmessage_length, 0, Diags);
1227 Opts.DumpBuildInformation = Args.getLastArgValue(OPT_dump_build_information);
1228 Opts.Warnings = Args.getAllArgValues(OPT_W);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001229}
1230
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001231static void ParseFileSystemArgs(FileSystemOptions &Opts, ArgList &Args) {
1232 Opts.WorkingDir = Args.getLastArgValue(OPT_working_directory);
1233}
1234
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001235static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
David Blaikied6471f72011-09-25 23:23:43 +00001236 DiagnosticsEngine &Diags) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001237 using namespace cc1options;
1238 Opts.ProgramAction = frontend::ParseSyntaxOnly;
1239 if (const Arg *A = Args.getLastArg(OPT_Action_Group)) {
1240 switch (A->getOption().getID()) {
1241 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00001242 llvm_unreachable("Invalid option in group!");
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001243 case OPT_ast_dump:
1244 Opts.ProgramAction = frontend::ASTDump; break;
John McCallf3514242010-11-24 11:21:45 +00001245 case OPT_ast_dump_xml:
1246 Opts.ProgramAction = frontend::ASTDumpXML; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001247 case OPT_ast_print:
1248 Opts.ProgramAction = frontend::ASTPrint; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001249 case OPT_ast_view:
1250 Opts.ProgramAction = frontend::ASTView; break;
1251 case OPT_dump_raw_tokens:
1252 Opts.ProgramAction = frontend::DumpRawTokens; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001253 case OPT_dump_tokens:
1254 Opts.ProgramAction = frontend::DumpTokens; break;
1255 case OPT_S:
1256 Opts.ProgramAction = frontend::EmitAssembly; break;
1257 case OPT_emit_llvm_bc:
1258 Opts.ProgramAction = frontend::EmitBC; break;
1259 case OPT_emit_html:
1260 Opts.ProgramAction = frontend::EmitHTML; break;
1261 case OPT_emit_llvm:
1262 Opts.ProgramAction = frontend::EmitLLVM; break;
1263 case OPT_emit_llvm_only:
1264 Opts.ProgramAction = frontend::EmitLLVMOnly; break;
Daniel Dunbar32148ce2010-05-25 18:41:01 +00001265 case OPT_emit_codegen_only:
1266 Opts.ProgramAction = frontend::EmitCodeGenOnly; break;
Daniel Dunbarda1573f2010-02-03 01:18:43 +00001267 case OPT_emit_obj:
1268 Opts.ProgramAction = frontend::EmitObj; break;
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +00001269 case OPT_fixit_EQ:
1270 Opts.FixItSuffix = A->getValue(Args);
1271 // fall-through!
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001272 case OPT_fixit:
1273 Opts.ProgramAction = frontend::FixIt; break;
Douglas Gregor467dc882011-08-25 22:30:56 +00001274 case OPT_emit_module:
1275 Opts.ProgramAction = frontend::GenerateModule; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001276 case OPT_emit_pch:
1277 Opts.ProgramAction = frontend::GeneratePCH; break;
1278 case OPT_emit_pth:
1279 Opts.ProgramAction = frontend::GeneratePTH; break;
Daniel Dunbar27585952010-03-19 19:44:04 +00001280 case OPT_init_only:
1281 Opts.ProgramAction = frontend::InitOnly; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001282 case OPT_fsyntax_only:
1283 Opts.ProgramAction = frontend::ParseSyntaxOnly; break;
1284 case OPT_print_decl_contexts:
1285 Opts.ProgramAction = frontend::PrintDeclContext; break;
Douglas Gregorf033f1d2010-07-20 20:18:03 +00001286 case OPT_print_preamble:
1287 Opts.ProgramAction = frontend::PrintPreamble; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001288 case OPT_E:
1289 Opts.ProgramAction = frontend::PrintPreprocessedInput; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001290 case OPT_rewrite_macros:
1291 Opts.ProgramAction = frontend::RewriteMacros; break;
1292 case OPT_rewrite_objc:
1293 Opts.ProgramAction = frontend::RewriteObjC; break;
1294 case OPT_rewrite_test:
1295 Opts.ProgramAction = frontend::RewriteTest; break;
1296 case OPT_analyze:
1297 Opts.ProgramAction = frontend::RunAnalysis; break;
1298 case OPT_Eonly:
1299 Opts.ProgramAction = frontend::RunPreprocessorOnly; break;
1300 }
1301 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +00001302
1303 if (const Arg* A = Args.getLastArg(OPT_plugin)) {
1304 Opts.Plugins.push_back(A->getValue(Args,0));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001305 Opts.ProgramAction = frontend::PluginAction;
1306 Opts.ActionName = A->getValue(Args);
Daniel Dunbar3177aae2010-06-16 16:59:23 +00001307
1308 for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg),
1309 end = Args.filtered_end(); it != end; ++it) {
1310 if ((*it)->getValue(Args, 0) == Opts.ActionName)
1311 Opts.PluginArgs.push_back((*it)->getValue(Args, 1));
1312 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001313 }
1314
Nico Weber5aa74af2011-01-25 20:34:14 +00001315 Opts.AddPluginActions = Args.getAllArgValues(OPT_add_plugin);
Nico Weberf25649c2011-01-29 21:21:49 +00001316 Opts.AddPluginArgs.resize(Opts.AddPluginActions.size());
1317 for (int i = 0, e = Opts.AddPluginActions.size(); i != e; ++i) {
1318 for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg),
1319 end = Args.filtered_end(); it != end; ++it) {
1320 if ((*it)->getValue(Args, 0) == Opts.AddPluginActions[i])
1321 Opts.AddPluginArgs[i].push_back((*it)->getValue(Args, 1));
1322 }
1323 }
Nico Weber5aa74af2011-01-25 20:34:14 +00001324
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001325 if (const Arg *A = Args.getLastArg(OPT_code_completion_at)) {
1326 Opts.CodeCompletionAt =
1327 ParsedSourceLocation::FromString(A->getValue(Args));
1328 if (Opts.CodeCompletionAt.FileName.empty())
1329 Diags.Report(diag::err_drv_invalid_value)
1330 << A->getAsString(Args) << A->getValue(Args);
1331 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001332 Opts.DisableFree = Args.hasArg(OPT_disable_free);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001333
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001334 Opts.OutputFile = Args.getLastArgValue(OPT_o);
1335 Opts.Plugins = Args.getAllArgValues(OPT_load);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001336 Opts.RelocatablePCH = Args.hasArg(OPT_relocatable_pch);
Daniel Dunbar9b5e9ae2009-12-03 07:01:58 +00001337 Opts.ShowHelp = Args.hasArg(OPT_help);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001338 Opts.ShowMacrosInCodeCompletion = Args.hasArg(OPT_code_completion_macros);
Douglas Gregord8e8a582010-05-25 21:41:55 +00001339 Opts.ShowCodePatternsInCodeCompletion
1340 = Args.hasArg(OPT_code_completion_patterns);
Douglas Gregor8071e422010-08-15 06:18:01 +00001341 Opts.ShowGlobalSymbolsInCodeCompletion
1342 = !Args.hasArg(OPT_no_code_completion_globals);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001343 Opts.ShowStats = Args.hasArg(OPT_print_stats);
1344 Opts.ShowTimers = Args.hasArg(OPT_ftime_report);
Daniel Dunbar9b5e9ae2009-12-03 07:01:58 +00001345 Opts.ShowVersion = Args.hasArg(OPT_version);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001346 Opts.ASTMergeFiles = Args.getAllArgValues(OPT_ast_merge);
1347 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
Nick Lewycky1450f262010-08-13 17:31:00 +00001348 Opts.FixWhatYouCan = Args.hasArg(OPT_fix_what_you_can);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001349
John McCall8f0e8d22011-06-15 23:25:17 +00001350 Opts.ARCMTAction = FrontendOptions::ARCMT_None;
1351 if (const Arg *A = Args.getLastArg(OPT_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00001352 OPT_arcmt_modify,
1353 OPT_arcmt_migrate)) {
John McCall8f0e8d22011-06-15 23:25:17 +00001354 switch (A->getOption().getID()) {
1355 default:
1356 llvm_unreachable("missed a case");
1357 case OPT_arcmt_check:
1358 Opts.ARCMTAction = FrontendOptions::ARCMT_Check;
1359 break;
1360 case OPT_arcmt_modify:
1361 Opts.ARCMTAction = FrontendOptions::ARCMT_Modify;
1362 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00001363 case OPT_arcmt_migrate:
1364 Opts.ARCMTAction = FrontendOptions::ARCMT_Migrate;
1365 break;
John McCall8f0e8d22011-06-15 23:25:17 +00001366 }
1367 }
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +00001368 Opts.ARCMTMigrateDir = Args.getLastArgValue(OPT_arcmt_migrate_directory);
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +00001369 Opts.ARCMTMigrateReportOut
1370 = Args.getLastArgValue(OPT_arcmt_migrate_report_output);
1371 Opts.ARCMTMigrateEmitARCErrors
1372 = Args.hasArg(OPT_arcmt_migrate_emit_arc_errors);
John McCall8f0e8d22011-06-15 23:25:17 +00001373
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001374 InputKind DashX = IK_None;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001375 if (const Arg *A = Args.getLastArg(OPT_x)) {
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001376 DashX = llvm::StringSwitch<InputKind>(A->getValue(Args))
1377 .Case("c", IK_C)
1378 .Case("cl", IK_OpenCL)
Peter Collingbourne895fcca2010-12-01 03:15:20 +00001379 .Case("cuda", IK_CUDA)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001380 .Case("c++", IK_CXX)
1381 .Case("objective-c", IK_ObjC)
1382 .Case("objective-c++", IK_ObjCXX)
1383 .Case("cpp-output", IK_PreprocessedC)
1384 .Case("assembler-with-cpp", IK_Asm)
1385 .Case("c++-cpp-output", IK_PreprocessedCXX)
1386 .Case("objective-c-cpp-output", IK_PreprocessedObjC)
Chris Lattner919f2ca2011-04-09 07:09:31 +00001387 .Case("objc-cpp-output", IK_PreprocessedObjC)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001388 .Case("objective-c++-cpp-output", IK_PreprocessedObjCXX)
Nico Weber1f7957d2011-08-13 19:03:50 +00001389 .Case("objc++-cpp-output", IK_PreprocessedObjCXX)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001390 .Case("c-header", IK_C)
Peter Collingbourneae57ead2011-10-09 22:03:19 +00001391 .Case("cl-header", IK_OpenCL)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001392 .Case("objective-c-header", IK_ObjC)
1393 .Case("c++-header", IK_CXX)
1394 .Case("objective-c++-header", IK_ObjCXX)
1395 .Case("ast", IK_AST)
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +00001396 .Case("ir", IK_LLVM_IR)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001397 .Default(IK_None);
1398 if (DashX == IK_None)
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001399 Diags.Report(diag::err_drv_invalid_value)
1400 << A->getAsString(Args) << A->getValue(Args);
1401 }
1402
1403 // '-' is the default input if none is given.
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001404 std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001405 Opts.Inputs.clear();
1406 if (Inputs.empty())
1407 Inputs.push_back("-");
1408 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001409 InputKind IK = DashX;
1410 if (IK == IK_None) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001411 IK = FrontendOptions::getInputKindForExtension(
Chris Lattner5f9e2722011-07-23 10:55:15 +00001412 StringRef(Inputs[i]).rsplit('.').second);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001413 // FIXME: Remove this hack.
1414 if (i == 0)
1415 DashX = IK;
1416 }
1417 Opts.Inputs.push_back(std::make_pair(IK, Inputs[i]));
1418 }
1419
1420 return DashX;
1421}
1422
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +00001423std::string CompilerInvocation::GetResourcesPath(const char *Argv0,
1424 void *MainAddr) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001425 llvm::sys::Path P = llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
1426
1427 if (!P.isEmpty()) {
1428 P.eraseComponent(); // Remove /clang from foo/bin/clang
1429 P.eraseComponent(); // Remove /bin from foo/bin
1430
1431 // Get foo/lib/clang/<version>/include
1432 P.appendComponent("lib");
1433 P.appendComponent("clang");
1434 P.appendComponent(CLANG_VERSION_STRING);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001435 }
1436
1437 return P.str();
1438}
1439
Daniel Dunbar1e69fe32009-12-13 03:45:58 +00001440static void ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001441 using namespace cc1options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001442 Opts.Sysroot = Args.getLastArgValue(OPT_isysroot, "/");
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001443 Opts.Verbose = Args.hasArg(OPT_v);
Francois Pichetc91562c2011-10-10 20:34:10 +00001444 Opts.UseBuiltinIncludes = !Args.hasArg(OPT_nobuiltininc);
Daniel Dunbara268fc02011-10-11 18:20:10 +00001445 Opts.UseStandardSystemIncludes = !Args.hasArg(OPT_nostdsysteminc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00001446 Opts.UseStandardCXXIncludes = !Args.hasArg(OPT_nostdincxx);
Bob Wilson13c4f212011-06-21 21:12:29 +00001447 if (const Arg *A = Args.getLastArg(OPT_stdlib_EQ))
1448 Opts.UseLibcxx = (strcmp(A->getValue(Args), "libc++") == 0);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001449 Opts.ResourceDir = Args.getLastArgValue(OPT_resource_dir);
Douglas Gregor9a6da692011-09-12 20:41:59 +00001450 Opts.ModuleCachePath = Args.getLastArgValue(OPT_fmodule_cache_path);
Douglas Gregor6e975c42011-09-13 23:15:45 +00001451 Opts.DisableModuleHash = Args.hasArg(OPT_fdisable_module_hash);
Douglas Gregor9a6da692011-09-12 20:41:59 +00001452
Douglas Gregor65e02fa2011-07-28 04:45:53 +00001453 // Add -I..., -F..., and -index-header-map options in order.
1454 bool IsIndexHeaderMap = false;
1455 for (arg_iterator it = Args.filtered_begin(OPT_I, OPT_F,
1456 OPT_index_header_map),
1457 ie = Args.filtered_end(); it != ie; ++it) {
1458 if ((*it)->getOption().matches(OPT_index_header_map)) {
1459 // -index-header-map applies to the next -I or -F.
1460 IsIndexHeaderMap = true;
1461 continue;
1462 }
1463
1464 frontend::IncludeDirGroup Group
1465 = IsIndexHeaderMap? frontend::IndexHeaderMap : frontend::Angled;
1466
1467 Opts.AddPath((*it)->getValue(Args), Group, true,
Bob Wilson5dd45f12011-06-21 21:53:08 +00001468 /*IsFramework=*/ (*it)->getOption().matches(OPT_F), false);
Douglas Gregor65e02fa2011-07-28 04:45:53 +00001469 IsIndexHeaderMap = false;
1470 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001471
1472 // Add -iprefix/-iwith-prefix/-iwithprefixbefore options.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001473 StringRef Prefix = ""; // FIXME: This isn't the correct default prefix.
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001474 for (arg_iterator it = Args.filtered_begin(OPT_iprefix, OPT_iwithprefix,
1475 OPT_iwithprefixbefore),
1476 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001477 const Arg *A = *it;
1478 if (A->getOption().matches(OPT_iprefix))
1479 Prefix = A->getValue(Args);
1480 else if (A->getOption().matches(OPT_iwithprefix))
1481 Opts.AddPath(Prefix.str() + A->getValue(Args),
Bob Wilson5dd45f12011-06-21 21:53:08 +00001482 frontend::System, false, false, false);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001483 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001484 Opts.AddPath(Prefix.str() + A->getValue(Args),
Bob Wilson5dd45f12011-06-21 21:53:08 +00001485 frontend::Angled, false, false, false);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001486 }
1487
1488 for (arg_iterator it = Args.filtered_begin(OPT_idirafter),
1489 ie = Args.filtered_end(); it != ie; ++it)
Bob Wilson5dd45f12011-06-21 21:53:08 +00001490 Opts.AddPath((*it)->getValue(Args), frontend::After, true, false, false);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001491 for (arg_iterator it = Args.filtered_begin(OPT_iquote),
1492 ie = Args.filtered_end(); it != ie; ++it)
Bob Wilson5dd45f12011-06-21 21:53:08 +00001493 Opts.AddPath((*it)->getValue(Args), frontend::Quoted, true, false, false);
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001494 for (arg_iterator it = Args.filtered_begin(OPT_isystem,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +00001495 OPT_iwithsysroot), ie = Args.filtered_end(); it != ie; ++it)
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001496 Opts.AddPath((*it)->getValue(Args), frontend::System, true, false,
1497 !(*it)->getOption().matches(OPT_iwithsysroot));
Daniel Dunbar024f4be2011-10-18 20:40:38 +00001498 for (arg_iterator it = Args.filtered_begin(OPT_iframework),
1499 ie = Args.filtered_end(); it != ie; ++it)
1500 Opts.AddPath((*it)->getValue(Args), frontend::System, true, true,
1501 true);
Benjamin Kramer47adebe2011-09-22 21:41:16 +00001502
1503 // Add the paths for the various language specific isystem flags.
1504 for (arg_iterator it = Args.filtered_begin(OPT_c_isystem),
1505 ie = Args.filtered_end(); it != ie; ++it)
1506 Opts.AddPath((*it)->getValue(Args), frontend::CSystem, true, false, true);
1507 for (arg_iterator it = Args.filtered_begin(OPT_cxx_isystem),
1508 ie = Args.filtered_end(); it != ie; ++it)
1509 Opts.AddPath((*it)->getValue(Args), frontend::CXXSystem, true, false, true);
1510 for (arg_iterator it = Args.filtered_begin(OPT_objc_isystem),
1511 ie = Args.filtered_end(); it != ie; ++it)
1512 Opts.AddPath((*it)->getValue(Args), frontend::ObjCSystem, true, false,true);
1513 for (arg_iterator it = Args.filtered_begin(OPT_objcxx_isystem),
1514 ie = Args.filtered_end(); it != ie; ++it)
1515 Opts.AddPath((*it)->getValue(Args), frontend::ObjCXXSystem, true, false,
1516 true);
Chandler Carruthac2bc4d2011-11-05 08:30:29 +00001517
1518 // Add the internal paths from a driver that detects standard include paths.
1519 for (arg_iterator I = Args.filtered_begin(OPT_internal_isystem,
Chandler Carruth07643082011-11-07 09:17:31 +00001520 OPT_internal_externc_isystem),
Chandler Carruthac2bc4d2011-11-05 08:30:29 +00001521 E = Args.filtered_end();
1522 I != E; ++I)
1523 Opts.AddPath((*I)->getValue(Args), frontend::System,
Chandler Carruth07643082011-11-07 09:17:31 +00001524 false, false, /*IgnoreSysRoot=*/true, /*IsInternal=*/true,
Chandler Carruthac2bc4d2011-11-05 08:30:29 +00001525 (*I)->getOption().matches(OPT_internal_externc_isystem));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001526}
1527
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001528void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,
1529 LangStandard::Kind LangStd) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001530 // Set some properties which depend solely on the input kind; it would be nice
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001531 // to move these to the language standard, and have the driver resolve the
1532 // input kind + language standard.
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001533 if (IK == IK_Asm) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001534 Opts.AsmPreprocessor = 1;
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001535 } else if (IK == IK_ObjC ||
1536 IK == IK_ObjCXX ||
1537 IK == IK_PreprocessedObjC ||
1538 IK == IK_PreprocessedObjCXX) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001539 Opts.ObjC1 = Opts.ObjC2 = 1;
1540 }
1541
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001542 if (LangStd == LangStandard::lang_unspecified) {
1543 // Based on the base language, pick one.
1544 switch (IK) {
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001545 case IK_None:
1546 case IK_AST:
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +00001547 case IK_LLVM_IR:
David Blaikieb219cfc2011-09-23 05:06:16 +00001548 llvm_unreachable("Invalid input kind!");
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001549 case IK_OpenCL:
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001550 LangStd = LangStandard::lang_opencl;
1551 break;
Peter Collingbourne895fcca2010-12-01 03:15:20 +00001552 case IK_CUDA:
1553 LangStd = LangStandard::lang_cuda;
1554 break;
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001555 case IK_Asm:
1556 case IK_C:
1557 case IK_PreprocessedC:
1558 case IK_ObjC:
1559 case IK_PreprocessedObjC:
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001560 LangStd = LangStandard::lang_gnu99;
1561 break;
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001562 case IK_CXX:
1563 case IK_PreprocessedCXX:
1564 case IK_ObjCXX:
1565 case IK_PreprocessedObjCXX:
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001566 LangStd = LangStandard::lang_gnucxx98;
1567 break;
1568 }
1569 }
1570
1571 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
1572 Opts.BCPLComment = Std.hasBCPLComments();
1573 Opts.C99 = Std.isC99();
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001574 Opts.C1X = Std.isC1X();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001575 Opts.CPlusPlus = Std.isCPlusPlus();
1576 Opts.CPlusPlus0x = Std.isCPlusPlus0x();
1577 Opts.Digraphs = Std.hasDigraphs();
1578 Opts.GNUMode = Std.isGNUMode();
1579 Opts.GNUInline = !Std.isC99();
1580 Opts.HexFloats = Std.hasHexFloats();
1581 Opts.ImplicitInt = Std.hasImplicitInt();
1582
1583 // OpenCL has some additional defaults.
1584 if (LangStd == LangStandard::lang_opencl) {
1585 Opts.OpenCL = 1;
Tobias Grosser37c31c22011-09-21 18:28:29 +00001586 Opts.AltiVec = 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001587 Opts.CXXOperatorNames = 1;
Tobias Grosser37c31c22011-09-21 18:28:29 +00001588 Opts.LaxVectorConversions = 0;
Peter Collingbournef315fa82011-02-14 01:42:53 +00001589 Opts.DefaultFPContract = 1;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001590 }
1591
Peter Collingbourne895fcca2010-12-01 03:15:20 +00001592 if (LangStd == LangStandard::lang_cuda)
1593 Opts.CUDA = 1;
1594
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001595 // OpenCL and C++ both have bool, true, false keywords.
1596 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
1597
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001598 Opts.GNUKeywords = Opts.GNUMode;
1599 Opts.CXXOperatorNames = Opts.CPlusPlus;
1600
1601 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
1602 // is specified, or -std is set to a conforming mode.
1603 Opts.Trigraphs = !Opts.GNUMode;
1604
1605 Opts.DollarIdents = !Opts.AsmPreprocessor;
1606}
1607
1608static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
David Blaikied6471f72011-09-25 23:23:43 +00001609 DiagnosticsEngine &Diags) {
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001610 // FIXME: Cleanup per-file based stuff.
1611 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
1612 if (const Arg *A = Args.getLastArg(OPT_std_EQ)) {
1613 LangStd = llvm::StringSwitch<LangStandard::Kind>(A->getValue(Args))
1614#define LANGSTANDARD(id, name, desc, features) \
1615 .Case(name, LangStandard::lang_##id)
1616#include "clang/Frontend/LangStandards.def"
1617 .Default(LangStandard::lang_unspecified);
1618 if (LangStd == LangStandard::lang_unspecified)
1619 Diags.Report(diag::err_drv_invalid_value)
1620 << A->getAsString(Args) << A->getValue(Args);
Chad Rosierb2dba4b2011-05-02 19:24:53 +00001621 else {
1622 // Valid standard, check to make sure language and standard are compatable.
1623 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
1624 switch (IK) {
1625 case IK_C:
1626 case IK_ObjC:
1627 case IK_PreprocessedC:
1628 case IK_PreprocessedObjC:
1629 if (!(Std.isC89() || Std.isC99()))
1630 Diags.Report(diag::err_drv_argument_not_allowed_with)
1631 << A->getAsString(Args) << "C/ObjC";
1632 break;
1633 case IK_CXX:
1634 case IK_ObjCXX:
1635 case IK_PreprocessedCXX:
1636 case IK_PreprocessedObjCXX:
1637 if (!Std.isCPlusPlus())
1638 Diags.Report(diag::err_drv_argument_not_allowed_with)
1639 << A->getAsString(Args) << "C++/ObjC++";
1640 break;
1641 case IK_OpenCL:
1642 if (!Std.isC99())
1643 Diags.Report(diag::err_drv_argument_not_allowed_with)
1644 << A->getAsString(Args) << "OpenCL";
1645 break;
1646 case IK_CUDA:
1647 if (!Std.isCPlusPlus())
1648 Diags.Report(diag::err_drv_argument_not_allowed_with)
1649 << A->getAsString(Args) << "CUDA";
1650 break;
1651 default:
1652 break;
1653 }
1654 }
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001655 }
1656
Peter Collingbourne91252d12010-12-04 01:51:40 +00001657 if (const Arg *A = Args.getLastArg(OPT_cl_std_EQ)) {
1658 if (strcmp(A->getValue(Args), "CL1.1") != 0) {
1659 Diags.Report(diag::err_drv_invalid_value)
1660 << A->getAsString(Args) << A->getValue(Args);
1661 }
1662 }
1663
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001664 CompilerInvocation::setLangDefaults(Opts, IK, LangStd);
1665
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001666 // We abuse '-f[no-]gnu-keywords' to force overriding all GNU-extension
1667 // keywords. This behavior is provided by GCC's poorly named '-fasm' flag,
1668 // while a subset (the non-C++ GNU keywords) is provided by GCC's
1669 // '-fgnu-keywords'. Clang conflates the two for simplicity under the single
1670 // name, as it doesn't seem a useful distinction.
1671 Opts.GNUKeywords = Args.hasFlag(OPT_fgnu_keywords, OPT_fno_gnu_keywords,
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001672 Opts.GNUKeywords);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001673
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001674 if (Args.hasArg(OPT_fno_operator_names))
1675 Opts.CXXOperatorNames = 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001676
John McCallf85e1932011-06-15 23:02:42 +00001677 if (Opts.ObjC1) {
1678 if (Args.hasArg(OPT_fobjc_gc_only))
Douglas Gregore289d812011-09-13 17:21:33 +00001679 Opts.setGC(LangOptions::GCOnly);
John McCallf85e1932011-06-15 23:02:42 +00001680 else if (Args.hasArg(OPT_fobjc_gc))
Douglas Gregore289d812011-09-13 17:21:33 +00001681 Opts.setGC(LangOptions::HybridGC);
John McCallf85e1932011-06-15 23:02:42 +00001682 else if (Args.hasArg(OPT_fobjc_arc)) {
1683 Opts.ObjCAutoRefCount = 1;
John McCalld1e40d52011-10-02 01:16:38 +00001684 if (Args.hasArg(OPT_fobjc_fragile_abi))
John McCallf85e1932011-06-15 23:02:42 +00001685 Diags.Report(diag::err_arc_nonfragile_abi);
John McCallf85e1932011-06-15 23:02:42 +00001686 }
1687
John McCall9f084a32011-07-06 00:26:06 +00001688 if (Args.hasArg(OPT_fobjc_runtime_has_weak))
1689 Opts.ObjCRuntimeHasWeak = 1;
1690
John McCallf85e1932011-06-15 23:02:42 +00001691 if (Args.hasArg(OPT_fno_objc_infer_related_result_type))
1692 Opts.ObjCInferRelatedResultType = 0;
1693 }
1694
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00001695 if (Args.hasArg(OPT_fgnu89_inline))
1696 Opts.GNUInline = 1;
1697
Fariborz Jahanian142f9e92011-02-04 00:01:24 +00001698 if (Args.hasArg(OPT_fapple_kext)) {
1699 if (!Opts.CPlusPlus)
1700 Diags.Report(diag::warn_c_kext);
1701 else
1702 Opts.AppleKext = 1;
1703 }
1704
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001705 if (Args.hasArg(OPT_print_ivar_layout))
1706 Opts.ObjCGCBitmapPrint = 1;
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001707 if (Args.hasArg(OPT_fno_constant_cfstrings))
1708 Opts.NoConstantCFStrings = 1;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001709
1710 if (Args.hasArg(OPT_faltivec))
1711 Opts.AltiVec = 1;
1712
1713 if (Args.hasArg(OPT_pthread))
1714 Opts.POSIXThreads = 1;
1715
Francois Pichet8387e2a2011-04-22 22:18:13 +00001716 if (Args.hasArg(OPT_fdelayed_template_parsing))
1717 Opts.DelayedTemplateParsing = 1;
1718
Chris Lattner5f9e2722011-07-23 10:55:15 +00001719 StringRef Vis = Args.getLastArgValue(OPT_fvisibility, "default");
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001720 if (Vis == "default")
John McCall1fb0caa2010-10-22 21:05:15 +00001721 Opts.setVisibilityMode(DefaultVisibility);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001722 else if (Vis == "hidden")
John McCall1fb0caa2010-10-22 21:05:15 +00001723 Opts.setVisibilityMode(HiddenVisibility);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001724 else if (Vis == "protected")
John McCall1fb0caa2010-10-22 21:05:15 +00001725 Opts.setVisibilityMode(ProtectedVisibility);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001726 else
1727 Diags.Report(diag::err_drv_invalid_value)
1728 << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
1729
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001730 if (Args.hasArg(OPT_fvisibility_inlines_hidden))
1731 Opts.InlineVisibilityHidden = 1;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001732
David Chisnall7f18e672010-09-17 18:29:54 +00001733 if (Args.hasArg(OPT_ftrapv)) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00001734 Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping);
David Chisnall7f18e672010-09-17 18:29:54 +00001735 // Set the handler, if one is specified.
1736 Opts.OverflowHandler =
1737 Args.getLastArgValue(OPT_ftrapv_handler);
1738 }
Chris Lattnera4d71452010-06-26 21:25:03 +00001739 else if (Args.hasArg(OPT_fwrapv))
Michael J. Spencer20249a12010-10-21 03:16:25 +00001740 Opts.setSignedOverflowBehavior(LangOptions::SOB_Defined);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001741
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001742 if (Args.hasArg(OPT_trigraphs))
1743 Opts.Trigraphs = 1;
1744
Daniel Dunbar8663b182009-12-16 20:10:18 +00001745 Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
1746 OPT_fno_dollars_in_identifiers,
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001747 Opts.DollarIdents);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001748 Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
Douglas Gregor0bb935c2011-10-24 15:27:23 +00001749 Opts.MicrosoftExt
1750 = Args.hasArg(OPT_fms_extensions) || Args.hasArg(OPT_fms_compatibility);
Francois Pichetae556082011-09-17 04:32:15 +00001751 Opts.MicrosoftMode = Args.hasArg(OPT_fms_compatibility);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001752 Opts.MSCVersion = Args.getLastArgIntValue(OPT_fmsc_version, 0, Diags);
Dawn Perchik400b6072010-09-02 23:59:25 +00001753 Opts.Borland = Args.hasArg(OPT_fborland_extensions);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001754 Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
Chandler Carruth50465d12011-04-23 06:30:43 +00001755 Opts.ConstStrings = Args.hasFlag(OPT_fconst_strings, OPT_fno_const_strings,
1756 Opts.ConstStrings);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001757 if (Args.hasArg(OPT_fno_lax_vector_conversions))
Anders Carlssona508b7d2010-02-06 23:23:06 +00001758 Opts.LaxVectorConversions = 0;
1759 if (Args.hasArg(OPT_fno_threadsafe_statics))
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001760 Opts.ThreadsafeStatics = 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001761 Opts.Exceptions = Args.hasArg(OPT_fexceptions);
Anders Carlssone9b801f2011-02-22 01:52:06 +00001762 Opts.ObjCExceptions = Args.hasArg(OPT_fobjc_exceptions);
Anders Carlsson0761e442011-02-23 03:16:42 +00001763 Opts.CXXExceptions = Args.hasArg(OPT_fcxx_exceptions);
1764 Opts.SjLjExceptions = Args.hasArg(OPT_fsjlj_exceptions);
Daniel Dunbar2ed42282011-03-18 21:23:38 +00001765 Opts.TraditionalCPP = Args.hasArg(OPT_traditional_cpp);
Anders Carlsson0761e442011-02-23 03:16:42 +00001766
Mike Stumpde050572009-12-02 18:57:08 +00001767 Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001768 Opts.Blocks = Args.hasArg(OPT_fblocks);
John McCall13db5cf2011-09-09 20:41:01 +00001769 Opts.BlocksRuntimeOptional = Args.hasArg(OPT_fblocks_runtime_optional);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001770 Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
1771 Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001772 Opts.ShortEnums = Args.hasArg(OPT_fshort_enums);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001773 Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
1774 Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
Nuno Lopesfc284482009-12-16 16:59:22 +00001775 Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001776 Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
John McCall7002f4c2010-04-09 19:03:51 +00001777 Opts.AccessControl = !Args.hasArg(OPT_fno_access_control);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001778 Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
Dan Gohmanc31176d2010-01-08 02:20:44 +00001779 Opts.MathErrno = Args.hasArg(OPT_fmath_errno);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001780 Opts.InstantiationDepth = Args.getLastArgIntValue(OPT_ftemplate_depth, 1024,
Richard Smithc18c4232011-11-21 19:36:32 +00001781 Diags);
1782 Opts.ConstexprCallDepth = Args.getLastArgIntValue(OPT_fconstexpr_depth, 512,
1783 Diags);
Francois Pichet8387e2a2011-04-22 22:18:13 +00001784 Opts.DelayedTemplateParsing = Args.hasArg(OPT_fdelayed_template_parsing);
Argyrios Kyrtzidis1380a142010-11-18 00:20:36 +00001785 Opts.NumLargeByValueCopy = Args.getLastArgIntValue(OPT_Wlarge_by_value_copy,
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001786 0, Diags);
Douglas Gregor6f755502011-02-01 15:15:22 +00001787 Opts.MSBitfields = Args.hasArg(OPT_mms_bitfields);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001788 Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001789 Opts.ObjCConstantStringClass =
1790 Args.getLastArgValue(OPT_fconstant_string_class);
John McCalld1e40d52011-10-02 01:16:38 +00001791 Opts.ObjCNonFragileABI = !Args.hasArg(OPT_fobjc_fragile_abi);
Fariborz Jahanian112c3302011-01-04 20:05:20 +00001792 if (Opts.ObjCNonFragileABI)
1793 Opts.ObjCNonFragileABI2 = true;
Ted Kremenekc32647d2010-12-23 21:35:43 +00001794 Opts.ObjCDefaultSynthProperties =
1795 Args.hasArg(OPT_fobjc_default_synthesize_properties);
Mike Stump9c276ae2009-12-12 01:27:46 +00001796 Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001797 Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
Daniel Dunbar88934e82011-10-05 21:04:55 +00001798 Opts.PackStruct = Args.getLastArgIntValue(OPT_fpack_struct, 0, Diags);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001799 Opts.PICLevel = Args.getLastArgIntValue(OPT_pic_level, 0, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001800 Opts.Static = Args.hasArg(OPT_static_define);
Anders Carlsson46116ce2010-04-10 19:09:13 +00001801 Opts.DumpRecordLayouts = Args.hasArg(OPT_fdump_record_layouts);
Anders Carlsson046c2942010-04-17 20:15:18 +00001802 Opts.DumpVTableLayouts = Args.hasArg(OPT_fdump_vtable_layouts);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001803 Opts.SpellChecking = !Args.hasArg(OPT_fno_spell_checking);
Daniel Dunbarfb937b82010-04-15 15:06:22 +00001804 Opts.NoBitFieldTypeAlign = Args.hasArg(OPT_fno_bitfield_type_align);
Peter Collingbourne09821362010-12-04 01:50:56 +00001805 Opts.SinglePrecisionConstants = Args.hasArg(OPT_cl_single_precision_constant);
Peter Collingbournef0840822010-12-04 01:51:23 +00001806 Opts.FastRelaxedMath = Args.hasArg(OPT_cl_fast_relaxed_math);
Roman Divackycfe9af22011-03-01 17:40:53 +00001807 Opts.MRTD = Args.hasArg(OPT_mrtd);
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001808 Opts.FakeAddressSpaceMap = Args.hasArg(OPT_ffake_address_space_map);
John McCalla5fc4722011-04-09 22:50:59 +00001809 Opts.ParseUnknownAnytype = Args.hasArg(OPT_funknown_anytype);
John McCall48218c62011-07-13 17:56:40 +00001810 Opts.DebuggerSupport = Args.hasArg(OPT_fdebugger_support);
Kostya Serebryanyb6196882011-11-22 01:28:36 +00001811 Opts.AddressSanitizer = Args.hasArg(OPT_faddress_sanitizer);
Eli Friedman19bda3a2011-11-02 01:53:16 +00001812 Opts.ApplePragmaPack = Args.hasArg(OPT_fapple_pragma_pack);
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001813 Opts.CurrentModule = Args.getLastArgValue(OPT_fmodule_name);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001814
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00001815 // Record whether the __DEPRECATED define was requested.
1816 Opts.Deprecated = Args.hasFlag(OPT_fdeprecated_macro,
1817 OPT_fno_deprecated_macro,
1818 Opts.Deprecated);
1819
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001820 // FIXME: Eliminate this dependency.
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +00001821 unsigned Opt = getOptimizationLevel(Args, IK, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001822 Opts.Optimize = Opt != 0;
Rafael Espindola4c7ce362011-11-26 06:21:20 +00001823 Opts.OptimizeSize = Args.hasArg(OPT_Os) || Args.hasArg(OPT_Oz);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001824
1825 // This is the __NO_INLINE__ define, which just depends on things like the
1826 // optimization level and -fno-inline, not actually whether the backend has
1827 // inlining enabled.
1828 //
1829 // FIXME: This is affected by other options (-fno-inline).
1830 Opts.NoInline = !Opt;
1831
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001832 unsigned SSP = Args.getLastArgIntValue(OPT_stack_protector, 0, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001833 switch (SSP) {
1834 default:
1835 Diags.Report(diag::err_drv_invalid_value)
1836 << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
1837 break;
Douglas Gregore289d812011-09-13 17:21:33 +00001838 case 0: Opts.setStackProtector(LangOptions::SSPOff); break;
1839 case 1: Opts.setStackProtector(LangOptions::SSPOn); break;
1840 case 2: Opts.setStackProtector(LangOptions::SSPReq); break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001841 }
1842}
1843
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001844static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001845 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00001846 DiagnosticsEngine &Diags) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001847 using namespace cc1options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001848 Opts.ImplicitPCHInclude = Args.getLastArgValue(OPT_include_pch);
1849 Opts.ImplicitPTHInclude = Args.getLastArgValue(OPT_include_pth);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001850 if (const Arg *A = Args.getLastArg(OPT_token_cache))
1851 Opts.TokenCache = A->getValue(Args);
1852 else
1853 Opts.TokenCache = Opts.ImplicitPTHInclude;
1854 Opts.UsePredefines = !Args.hasArg(OPT_undef);
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001855 Opts.DetailedRecord = Args.hasArg(OPT_detailed_preprocessing_record);
Douglas Gregorfba18aa2011-09-15 22:00:41 +00001856 Opts.AutoModuleImport = Args.hasArg(OPT_fauto_module_import);
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001857 Opts.DisablePCHValidation = Args.hasArg(OPT_fno_validate_pch);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +00001858
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +00001859 Opts.DumpDeserializedPCHDecls = Args.hasArg(OPT_dump_deserialized_pch_decls);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +00001860 for (arg_iterator it = Args.filtered_begin(OPT_error_on_deserialized_pch_decl),
1861 ie = Args.filtered_end(); it != ie; ++it) {
1862 const Arg *A = *it;
1863 Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue(Args));
1864 }
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001865
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +00001866 if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001867 StringRef Value(A->getValue(Args));
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +00001868 size_t Comma = Value.find(',');
1869 unsigned Bytes = 0;
1870 unsigned EndOfLine = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001871
Chris Lattner5f9e2722011-07-23 10:55:15 +00001872 if (Comma == StringRef::npos ||
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +00001873 Value.substr(0, Comma).getAsInteger(10, Bytes) ||
1874 Value.substr(Comma + 1).getAsInteger(10, EndOfLine))
1875 Diags.Report(diag::err_drv_preamble_format);
1876 else {
1877 Opts.PrecompiledPreambleBytes.first = Bytes;
1878 Opts.PrecompiledPreambleBytes.second = (EndOfLine != 0);
1879 }
1880 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001881
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001882 // Add macros from the command line.
1883 for (arg_iterator it = Args.filtered_begin(OPT_D, OPT_U),
1884 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001885 if ((*it)->getOption().matches(OPT_D))
1886 Opts.addMacroDef((*it)->getValue(Args));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001887 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001888 Opts.addMacroUndef((*it)->getValue(Args));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001889 }
1890
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001891 Opts.MacroIncludes = Args.getAllArgValues(OPT_imacros);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001892
1893 // Add the ordered list of -includes.
1894 for (arg_iterator it = Args.filtered_begin(OPT_include, OPT_include_pch,
1895 OPT_include_pth),
1896 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001897 const Arg *A = *it;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001898 // PCH is handled specially, we need to extra the original include path.
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001899 if (A->getOption().matches(OPT_include_pch)) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001900 std::string OriginalFile =
Chris Lattner39b49bc2010-11-23 08:35:12 +00001901 ASTReader::getOriginalSourceFile(A->getValue(Args), FileMgr, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001902 if (OriginalFile.empty())
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00001903 continue;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001904
1905 Opts.Includes.push_back(OriginalFile);
1906 } else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001907 Opts.Includes.push_back(A->getValue(Args));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001908 }
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001909
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00001910 for (arg_iterator it = Args.filtered_begin(OPT_chain_include),
1911 ie = Args.filtered_end(); it != ie; ++it) {
1912 const Arg *A = *it;
1913 Opts.ChainedIncludes.push_back(A->getValue(Args));
1914 }
1915
Chris Lattnerdd173942010-04-14 03:54:58 +00001916 // Include 'altivec.h' if -faltivec option present
1917 if (Args.hasArg(OPT_faltivec))
1918 Opts.Includes.push_back("altivec.h");
1919
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001920 for (arg_iterator it = Args.filtered_begin(OPT_remap_file),
1921 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001922 const Arg *A = *it;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001923 std::pair<StringRef,StringRef> Split =
1924 StringRef(A->getValue(Args)).split(';');
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001925
1926 if (Split.second.empty()) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001927 Diags.Report(diag::err_drv_invalid_remap_file) << A->getAsString(Args);
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001928 continue;
1929 }
1930
1931 Opts.addRemappedFile(Split.first, Split.second);
1932 }
John McCallf85e1932011-06-15 23:02:42 +00001933
1934 if (Arg *A = Args.getLastArg(OPT_fobjc_arc_cxxlib_EQ)) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001935 StringRef Name = A->getValue(Args);
John McCallf85e1932011-06-15 23:02:42 +00001936 unsigned Library = llvm::StringSwitch<unsigned>(Name)
1937 .Case("libc++", ARCXX_libcxx)
1938 .Case("libstdc++", ARCXX_libstdcxx)
1939 .Case("none", ARCXX_nolib)
1940 .Default(~0U);
1941 if (Library == ~0U)
1942 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
1943 else
1944 Opts.ObjCXXARCStandardLibrary = (ObjCXXARCStandardLibraryKind)Library;
1945 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001946}
1947
1948static void ParsePreprocessorOutputArgs(PreprocessorOutputOptions &Opts,
1949 ArgList &Args) {
1950 using namespace cc1options;
1951 Opts.ShowCPP = !Args.hasArg(OPT_dM);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001952 Opts.ShowComments = Args.hasArg(OPT_C);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001953 Opts.ShowLineMarkers = !Args.hasArg(OPT_P);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001954 Opts.ShowMacroComments = Args.hasArg(OPT_CC);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001955 Opts.ShowMacros = Args.hasArg(OPT_dM) || Args.hasArg(OPT_dD);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001956}
1957
1958static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) {
1959 using namespace cc1options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001960 Opts.ABI = Args.getLastArgValue(OPT_target_abi);
Charles Davis98b7c5c2010-06-11 01:06:47 +00001961 Opts.CXXABI = Args.getLastArgValue(OPT_cxx_abi);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001962 Opts.CPU = Args.getLastArgValue(OPT_target_cpu);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001963 Opts.Features = Args.getAllArgValues(OPT_target_feature);
Daniel Dunbar39065a52010-08-11 23:07:42 +00001964 Opts.LinkerVersion = Args.getLastArgValue(OPT_target_linker_version);
Duncan Sands2dc14532010-08-30 09:42:39 +00001965 Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001966
Sebastian Pop5d8b9542011-11-01 21:33:06 +00001967 // Use the default target triple if unspecified.
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001968 if (Opts.Triple.empty())
Sebastian Pop5d8b9542011-11-01 21:33:06 +00001969 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001970}
1971
1972//
1973
1974void CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
Chris Lattner39b49bc2010-11-23 08:35:12 +00001975 const char *const *ArgBegin,
1976 const char *const *ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +00001977 DiagnosticsEngine &Diags) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001978 // Parse the arguments.
1979 llvm::OwningPtr<OptTable> Opts(createCC1OptTable());
1980 unsigned MissingArgIndex, MissingArgCount;
1981 llvm::OwningPtr<InputArgList> Args(
1982 Opts->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
1983
1984 // Check for missing argument error.
1985 if (MissingArgCount)
1986 Diags.Report(diag::err_drv_missing_argument)
1987 << Args->getArgString(MissingArgIndex) << MissingArgCount;
1988
1989 // Issue errors on unknown arguments.
1990 for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
1991 ie = Args->filtered_end(); it != ie; ++it)
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001992 Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001993
1994 ParseAnalyzerArgs(Res.getAnalyzerOpts(), *Args, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001995 ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), *Args);
1996 ParseDiagnosticArgs(Res.getDiagnosticOpts(), *Args, Diags);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001997 ParseFileSystemArgs(Res.getFileSystemOpts(), *Args);
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +00001998 // FIXME: We shouldn't have to pass the DashX option around here
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001999 InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags);
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +00002000 ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, DashX, Diags);
Daniel Dunbar1e69fe32009-12-13 03:45:58 +00002001 ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args);
Fariborz Jahaniane293d302011-02-25 17:24:55 +00002002 if (DashX != IK_AST && DashX != IK_LLVM_IR) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +00002003 ParseLangArgs(*Res.getLangOpts(), *Args, DashX, Diags);
Fariborz Jahaniane293d302011-02-25 17:24:55 +00002004 if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC)
Ted Kremenekd3b74d92011-11-17 23:01:24 +00002005 Res.getLangOpts()->ObjCExceptions = 1;
Fariborz Jahaniane293d302011-02-25 17:24:55 +00002006 }
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002007 // FIXME: ParsePreprocessorArgs uses the FileManager to read the contents of
2008 // PCH file and find the original header name. Remove the need to do that in
Chris Lattner39b49bc2010-11-23 08:35:12 +00002009 // ParsePreprocessorArgs and remove the FileManager
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002010 // parameters from the function and the "FileManager.h" #include.
Chris Lattner7ad97ff2010-11-23 07:51:02 +00002011 FileManager FileMgr(Res.getFileSystemOpts());
Chris Lattner39b49bc2010-11-23 08:35:12 +00002012 ParsePreprocessorArgs(Res.getPreprocessorOpts(), *Args, FileMgr, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00002013 ParsePreprocessorOutputArgs(Res.getPreprocessorOutputOpts(), *Args);
2014 ParseTargetArgs(Res.getTargetOpts(), *Args);
2015}
Douglas Gregor6e975c42011-09-13 23:15:45 +00002016
2017namespace {
2018
2019 class ModuleSignature {
2020 llvm::SmallVector<uint64_t, 16> Data;
2021 unsigned CurBit;
2022 uint64_t CurValue;
2023
2024 public:
2025 ModuleSignature() : CurBit(0), CurValue(0) { }
2026
2027 void add(uint64_t Value, unsigned Bits);
2028 void add(StringRef Value);
2029 void flush();
2030
2031 llvm::APInt getAsInteger() const;
2032 };
2033}
2034
2035void ModuleSignature::add(uint64_t Value, unsigned int NumBits) {
2036 CurValue |= Value << CurBit;
2037 if (CurBit + NumBits < 64) {
2038 CurBit += NumBits;
2039 return;
2040 }
2041
2042 // Add the current word.
2043 Data.push_back(CurValue);
2044
2045 if (CurBit)
2046 CurValue = Value >> (64-CurBit);
2047 else
2048 CurValue = 0;
2049 CurBit = (CurBit+NumBits) & 63;
2050}
2051
2052void ModuleSignature::flush() {
2053 if (CurBit == 0)
2054 return;
2055
2056 Data.push_back(CurValue);
2057 CurBit = 0;
2058 CurValue = 0;
2059}
2060
2061void ModuleSignature::add(StringRef Value) {
2062 for (StringRef::iterator I = Value.begin(), IEnd = Value.end(); I != IEnd;++I)
2063 add(*I, 8);
2064}
2065
2066llvm::APInt ModuleSignature::getAsInteger() const {
2067 return llvm::APInt(Data.size() * 64, Data);
2068}
2069
2070std::string CompilerInvocation::getModuleHash() const {
2071 ModuleSignature Signature;
2072
2073 // Start the signature with the compiler version.
2074 Signature.add(getClangFullRepositoryVersion());
2075
2076 // Extend the signature with the language options
2077#define LANGOPT(Name, Bits, Default, Description) \
Ted Kremenekd3b74d92011-11-17 23:01:24 +00002078 Signature.add(LangOpts->Name, Bits);
Douglas Gregor6e975c42011-09-13 23:15:45 +00002079#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
Ted Kremenekd3b74d92011-11-17 23:01:24 +00002080 Signature.add(static_cast<unsigned>(LangOpts->get##Name()), Bits);
Douglas Gregor6e975c42011-09-13 23:15:45 +00002081#define BENIGN_LANGOPT(Name, Bits, Default, Description)
2082#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
2083#include "clang/Basic/LangOptions.def"
2084
2085 // Extend the signature with the target triple
2086 llvm::Triple T(TargetOpts.Triple);
2087 Signature.add((unsigned)T.getArch(), 5);
2088 Signature.add((unsigned)T.getVendor(), 4);
2089 Signature.add((unsigned)T.getOS(), 5);
2090 Signature.add((unsigned)T.getEnvironment(), 4);
2091
Douglas Gregor815759c2011-09-14 15:55:12 +00002092 // Extend the signature with preprocessor options.
2093 Signature.add(getPreprocessorOpts().UsePredefines, 1);
2094 Signature.add(getPreprocessorOpts().DetailedRecord, 1);
2095
Douglas Gregor05edf662011-10-17 14:55:37 +00002096 // Hash the preprocessor defines.
2097 // FIXME: This is terrible. Use an MD5 sum of the preprocessor defines.
2098 std::vector<StringRef> MacroDefs;
2099 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
2100 I = getPreprocessorOpts().Macros.begin(),
2101 IEnd = getPreprocessorOpts().Macros.end();
2102 I != IEnd; ++I) {
2103 if (!I->second)
2104 MacroDefs.push_back(I->first);
2105 }
2106 llvm::array_pod_sort(MacroDefs.begin(), MacroDefs.end());
2107
2108 unsigned PPHashResult = 0;
2109 for (unsigned I = 0, N = MacroDefs.size(); I != N; ++I)
2110 PPHashResult = llvm::HashString(MacroDefs[I], PPHashResult);
2111 Signature.add(PPHashResult, 32);
2112
Douglas Gregor6e975c42011-09-13 23:15:45 +00002113 // We've generated the signature. Treat it as one large APInt that we'll
Douglas Gregor815759c2011-09-14 15:55:12 +00002114 // encode in base-36 and return.
Douglas Gregor6e975c42011-09-13 23:15:45 +00002115 Signature.flush();
Douglas Gregor815759c2011-09-14 15:55:12 +00002116 return Signature.getAsInteger().toString(36, /*Signed=*/false);
Douglas Gregor6e975c42011-09-13 23:15:45 +00002117}