blob: b200637cb57f7cc62d5f39d67c5f26d914b9c9e8 [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
Roman Divacky95f38f62012-12-21 17:23:52 +000010#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000011#include "clang/Basic/Diagnostic.h"
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000012#include "clang/Basic/FileManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000013#include "clang/Basic/Version.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000014#include "clang/Driver/Arg.h"
15#include "clang/Driver/ArgList.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000016#include "clang/Driver/DriverDiagnostic.h"
17#include "clang/Driver/OptTable.h"
18#include "clang/Driver/Option.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Driver/Options.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000020#include "clang/Frontend/LangStandard.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/HeaderSearchOptions.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000022#include "clang/Serialization/ASTReader.h"
Douglas Gregor984e2eb2012-11-05 19:45:09 +000023#include "llvm/ADT/Hashing.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000024#include "llvm/ADT/OwningPtr.h"
25#include "llvm/ADT/SmallVector.h"
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000026#include "llvm/ADT/StringExtras.h"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000027#include "llvm/ADT/StringSwitch.h"
Duncan Sands2dc14532010-08-30 09:42:39 +000028#include "llvm/ADT/Triple.h"
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000029#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000030#include "llvm/Support/Host.h"
31#include "llvm/Support/Path.h"
Daniel Dunbarb3375cb2009-11-17 06:02:29 +000032using namespace clang;
33
Ted Kremenekd3b74d92011-11-17 23:01:24 +000034//===----------------------------------------------------------------------===//
35// Initialization.
36//===----------------------------------------------------------------------===//
37
Ted Kremenek8a0c5c22011-11-18 04:32:13 +000038CompilerInvocationBase::CompilerInvocationBase()
Douglas Gregor02c23eb2012-10-23 22:26:28 +000039 : LangOpts(new LangOptions()), TargetOpts(new TargetOptions()),
Douglas Gregorc042edd2012-10-24 16:19:39 +000040 DiagnosticOpts(new DiagnosticOptions()),
Douglas Gregor14e71f02012-10-24 17:01:35 +000041 HeaderSearchOpts(new HeaderSearchOptions()),
42 PreprocessorOpts(new PreprocessorOptions()) {}
Ted Kremenekd3b74d92011-11-17 23:01:24 +000043
Ted Kremenek8a0c5c22011-11-18 04:32:13 +000044CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &X)
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000045 : RefCountedBase<CompilerInvocation>(),
Douglas Gregor57016dd2012-10-16 23:40:58 +000046 LangOpts(new LangOptions(*X.getLangOpts())),
Douglas Gregor02c23eb2012-10-23 22:26:28 +000047 TargetOpts(new TargetOptions(X.getTargetOpts())),
Douglas Gregorc042edd2012-10-24 16:19:39 +000048 DiagnosticOpts(new DiagnosticOptions(X.getDiagnosticOpts())),
Douglas Gregor14e71f02012-10-24 17:01:35 +000049 HeaderSearchOpts(new HeaderSearchOptions(X.getHeaderSearchOpts())),
50 PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())) {}
Ted Kremenekd3b74d92011-11-17 23:01:24 +000051
52//===----------------------------------------------------------------------===//
Richard Smith7c3f8b32012-11-01 03:48:49 +000053// Deserialization (from args)
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000054//===----------------------------------------------------------------------===//
55
56using namespace clang::driver;
James Molloybfd7a522012-05-01 14:57:16 +000057using namespace clang::driver::options;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000058
Daniel Dunbar56ac85c2009-12-01 03:16:53 +000059//
60
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +000061static unsigned getOptimizationLevel(ArgList &Args, InputKind IK,
David Blaikied6471f72011-09-25 23:23:43 +000062 DiagnosticsEngine &Diags) {
Peter Collingbourne61e07862010-12-04 01:50:45 +000063 unsigned DefaultOpt = 0;
64 if (IK == IK_OpenCL && !Args.hasArg(OPT_cl_opt_disable))
65 DefaultOpt = 2;
James Molloybfd7a522012-05-01 14:57:16 +000066
67 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
68 if (A->getOption().matches(options::OPT_O0))
69 return 0;
70
71 assert (A->getOption().matches(options::OPT_O));
72
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000073 StringRef S(A->getValue());
James Molloybfd7a522012-05-01 14:57:16 +000074 if (S == "s" || S == "z" || S.empty())
75 return 2;
76
77 return Args.getLastArgIntValue(OPT_O, DefaultOpt, Diags);
78 }
79
80 return DefaultOpt;
81}
82
83static unsigned getOptimizationLevelSize(ArgList &Args, InputKind IK,
84 DiagnosticsEngine &Diags) {
85 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
86 if (A->getOption().matches(options::OPT_O)) {
Richard Smith1d489cf2012-11-01 04:30:05 +000087 switch (A->getValue()[0]) {
James Molloybfd7a522012-05-01 14:57:16 +000088 default:
89 return 0;
90 case 's':
91 return 1;
92 case 'z':
93 return 2;
94 }
95 }
96 }
97 return 0;
98}
99
100static void addWarningArgs(ArgList &Args, std::vector<std::string> &Warnings) {
101 for (arg_iterator I = Args.filtered_begin(OPT_W_Group),
102 E = Args.filtered_end(); I != E; ++I) {
103 Arg *A = *I;
Michael J. Spencerc6357102012-10-22 22:13:48 +0000104 // If the argument is a pure flag, add its name (minus the "W" at the beginning)
James Molloybfd7a522012-05-01 14:57:16 +0000105 // to the warning list. Else, add its value (for the OPT_W case).
106 if (A->getOption().getKind() == Option::FlagClass) {
Michael J. Spencerc6357102012-10-22 22:13:48 +0000107 Warnings.push_back(A->getOption().getName().substr(1));
James Molloybfd7a522012-05-01 14:57:16 +0000108 } else {
109 for (unsigned Idx = 0, End = A->getNumValues();
110 Idx < End; ++Idx) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000111 StringRef V = A->getValue(Idx);
James Molloybfd7a522012-05-01 14:57:16 +0000112 // "-Wl," and such are not warning options.
113 // FIXME: Should be handled by putting these in separate flags.
114 if (V.startswith("l,") || V.startswith("a,") || V.startswith("p,"))
115 continue;
116
117 Warnings.push_back(V);
118 }
119 }
120 }
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +0000121}
122
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000123static bool ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args,
David Blaikied6471f72011-09-25 23:23:43 +0000124 DiagnosticsEngine &Diags) {
James Molloybfd7a522012-05-01 14:57:16 +0000125 using namespace options;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000126 bool Success = true;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000127 if (Arg *A = Args.getLastArg(OPT_analyzer_store)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000128 StringRef Name = A->getValue();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000129 AnalysisStores Value = llvm::StringSwitch<AnalysisStores>(Name)
130#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) \
131 .Case(CMDFLAG, NAME##Model)
Ted Kremenek987695a2012-08-31 04:35:58 +0000132#include "clang/StaticAnalyzer/Core/Analyses.def"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000133 .Default(NumStores);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000134 if (Value == NumStores) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000135 Diags.Report(diag::err_drv_invalid_value)
Daniel Dunbarbfbb39d2010-06-09 22:30:54 +0000136 << A->getAsString(Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000137 Success = false;
138 } else {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000139 Opts.AnalysisStoreOpt = Value;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000140 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000141 }
142
143 if (Arg *A = Args.getLastArg(OPT_analyzer_constraints)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000144 StringRef Name = A->getValue();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000145 AnalysisConstraints Value = llvm::StringSwitch<AnalysisConstraints>(Name)
146#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) \
147 .Case(CMDFLAG, NAME##Model)
Ted Kremenek987695a2012-08-31 04:35:58 +0000148#include "clang/StaticAnalyzer/Core/Analyses.def"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000149 .Default(NumConstraints);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000150 if (Value == NumConstraints) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000151 Diags.Report(diag::err_drv_invalid_value)
Daniel Dunbarbfbb39d2010-06-09 22:30:54 +0000152 << A->getAsString(Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000153 Success = false;
154 } else {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000155 Opts.AnalysisConstraintsOpt = Value;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000156 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000157 }
158
159 if (Arg *A = Args.getLastArg(OPT_analyzer_output)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000160 StringRef Name = A->getValue();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000161 AnalysisDiagClients Value = llvm::StringSwitch<AnalysisDiagClients>(Name)
162#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREAT) \
163 .Case(CMDFLAG, PD_##NAME)
Ted Kremenek987695a2012-08-31 04:35:58 +0000164#include "clang/StaticAnalyzer/Core/Analyses.def"
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000165 .Default(NUM_ANALYSIS_DIAG_CLIENTS);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000166 if (Value == NUM_ANALYSIS_DIAG_CLIENTS) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000167 Diags.Report(diag::err_drv_invalid_value)
Daniel Dunbarbfbb39d2010-06-09 22:30:54 +0000168 << A->getAsString(Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000169 Success = false;
170 } else {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000171 Opts.AnalysisDiagOpt = Value;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000172 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000173 }
174
Anna Zaksd3095282011-09-30 02:03:00 +0000175 if (Arg *A = Args.getLastArg(OPT_analyzer_purge)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000176 StringRef Name = A->getValue();
Anna Zaksd3095282011-09-30 02:03:00 +0000177 AnalysisPurgeMode Value = llvm::StringSwitch<AnalysisPurgeMode>(Name)
178#define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) \
179 .Case(CMDFLAG, NAME)
Ted Kremenek987695a2012-08-31 04:35:58 +0000180#include "clang/StaticAnalyzer/Core/Analyses.def"
Anna Zaksd3095282011-09-30 02:03:00 +0000181 .Default(NumPurgeModes);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000182 if (Value == NumPurgeModes) {
Anna Zaksd3095282011-09-30 02:03:00 +0000183 Diags.Report(diag::err_drv_invalid_value)
184 << A->getAsString(Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000185 Success = false;
186 } else {
Anna Zaksd3095282011-09-30 02:03:00 +0000187 Opts.AnalysisPurgeOpt = Value;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000188 }
Anna Zaksd3095282011-09-30 02:03:00 +0000189 }
190
Anna Zaks66253352012-03-08 23:16:35 +0000191 if (Arg *A = Args.getLastArg(OPT_analyzer_inlining_mode)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000192 StringRef Name = A->getValue();
Anna Zaks66253352012-03-08 23:16:35 +0000193 AnalysisInliningMode Value = llvm::StringSwitch<AnalysisInliningMode>(Name)
194#define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) \
195 .Case(CMDFLAG, NAME)
Ted Kremenek987695a2012-08-31 04:35:58 +0000196#include "clang/StaticAnalyzer/Core/Analyses.def"
Anna Zaks66253352012-03-08 23:16:35 +0000197 .Default(NumInliningModes);
198 if (Value == NumInliningModes) {
199 Diags.Report(diag::err_drv_invalid_value)
200 << A->getAsString(Args) << Name;
201 Success = false;
202 } else {
203 Opts.InliningMode = Value;
204 }
205 }
206
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000207 Opts.ShowCheckerHelp = Args.hasArg(OPT_analyzer_checker_help);
Ted Kremenek3b8a0402012-08-30 19:26:53 +0000208 Opts.visualizeExplodedGraphWithGraphViz =
209 Args.hasArg(OPT_analyzer_viz_egraph_graphviz);
210 Opts.visualizeExplodedGraphWithUbiGraph =
211 Args.hasArg(OPT_analyzer_viz_egraph_ubigraph);
Anna Zaksb47dbcb2012-03-28 19:59:16 +0000212 Opts.NoRetryExhausted = Args.hasArg(OPT_analyzer_disable_retry_exhausted);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000213 Opts.AnalyzeAll = Args.hasArg(OPT_analyzer_opt_analyze_headers);
214 Opts.AnalyzerDisplayProgress = Args.hasArg(OPT_analyzer_display_progress);
Ted Kremenekfc576512009-12-07 22:06:12 +0000215 Opts.AnalyzeNestedBlocks =
216 Args.hasArg(OPT_analyzer_opt_analyze_nested_blocks);
Ted Kremenek0caa2d42012-08-30 19:26:48 +0000217 Opts.eagerlyAssumeBinOpBifurcation = Args.hasArg(OPT_analyzer_eagerly_assume);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000218 Opts.AnalyzeSpecificFunction = Args.getLastArgValue(OPT_analyze_function);
Ted Kremenek9b823e82010-08-03 00:09:51 +0000219 Opts.UnoptimizedCFG = Args.hasArg(OPT_analysis_UnoptimizedCFG);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000220 Opts.TrimGraph = Args.hasArg(OPT_trim_egraph);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000221 Opts.MaxNodes = Args.getLastArgIntValue(OPT_analyzer_max_nodes, 150000,Diags);
Ted Kremenek2fa9d722012-08-30 19:26:56 +0000222 Opts.maxBlockVisitOnPath = Args.getLastArgIntValue(OPT_analyzer_max_loop, 4, Diags);
Anna Zaks81fb1692012-02-27 21:33:16 +0000223 Opts.PrintStats = Args.hasArg(OPT_analyzer_stats);
Anna Zaks8235f9c2012-03-02 19:05:03 +0000224 Opts.InlineMaxStackDepth =
225 Args.getLastArgIntValue(OPT_analyzer_inline_max_stack_depth,
226 Opts.InlineMaxStackDepth, Diags);
227 Opts.InlineMaxFunctionSize =
228 Args.getLastArgIntValue(OPT_analyzer_inline_max_function_size,
229 Opts.InlineMaxFunctionSize, Diags);
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000230
231 Opts.CheckersControlList.clear();
232 for (arg_iterator it = Args.filtered_begin(OPT_analyzer_checker,
233 OPT_analyzer_disable_checker),
234 ie = Args.filtered_end(); it != ie; ++it) {
235 const Arg *A = *it;
236 A->claim();
237 bool enable = (A->getOption().getID() == OPT_analyzer_checker);
Argyrios Kyrtzidis38c8fe72011-02-24 08:42:20 +0000238 // We can have a list of comma separated checker names, e.g:
239 // '-analyzer-checker=cocoa,unix'
Richard Smith1d489cf2012-11-01 04:30:05 +0000240 StringRef checkerList = A->getValue();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000241 SmallVector<StringRef, 4> checkers;
Argyrios Kyrtzidis38c8fe72011-02-24 08:42:20 +0000242 checkerList.split(checkers, ",");
243 for (unsigned i = 0, e = checkers.size(); i != e; ++i)
244 Opts.CheckersControlList.push_back(std::make_pair(checkers[i], enable));
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000245 }
Ted Kremenek7b73e082012-08-29 05:55:00 +0000246
247 // Go through the analyzer configuration options.
248 for (arg_iterator it = Args.filtered_begin(OPT_analyzer_config),
249 ie = Args.filtered_end(); it != ie; ++it) {
250 const Arg *A = *it;
251 A->claim();
252 // We can have a list of comma separated config names, e.g:
Ted Kremenek318cc3c2012-08-30 05:49:16 +0000253 // '-analyzer-config key1=val1,key2=val2'
Richard Smith1d489cf2012-11-01 04:30:05 +0000254 StringRef configList = A->getValue();
Ted Kremenek7b73e082012-08-29 05:55:00 +0000255 SmallVector<StringRef, 4> configVals;
256 configList.split(configVals, ",");
257 for (unsigned i = 0, e = configVals.size(); i != e; ++i) {
258 StringRef key, val;
Ted Kremenek318cc3c2012-08-30 05:49:16 +0000259 llvm::tie(key, val) = configVals[i].split("=");
Ted Kremenek7b73e082012-08-29 05:55:00 +0000260 if (val.empty()) {
261 Diags.Report(SourceLocation(),
262 diag::err_analyzer_config_no_value) << configVals[i];
263 Success = false;
264 break;
265 }
Ted Kremenek318cc3c2012-08-30 05:49:16 +0000266 if (val.find('=') != StringRef::npos) {
Ted Kremenek7b73e082012-08-29 05:55:00 +0000267 Diags.Report(SourceLocation(),
268 diag::err_analyzer_config_multiple_values)
269 << configVals[i];
270 Success = false;
271 break;
272 }
273 Opts.Config[key] = val;
274 }
275 }
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000276
277 return Success;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000278}
279
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000280static bool ParseMigratorArgs(MigratorOptions &Opts, ArgList &Args) {
281 Opts.NoNSAllocReallocError = Args.hasArg(OPT_migrator_no_nsalloc_error);
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000282 Opts.NoFinalizeRemoval = Args.hasArg(OPT_migrator_no_finalize_removal);
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000283 return true;
284}
285
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000286static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
David Blaikied6471f72011-09-25 23:23:43 +0000287 DiagnosticsEngine &Diags) {
James Molloybfd7a522012-05-01 14:57:16 +0000288 using namespace options;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000289 bool Success = true;
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +0000290
Aaron Ballmane6cc51d2012-02-06 00:40:31 +0000291 unsigned OptLevel = getOptimizationLevel(Args, IK, Diags);
292 if (OptLevel > 3) {
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +0000293 Diags.Report(diag::err_drv_invalid_value)
Aaron Ballmane6cc51d2012-02-06 00:40:31 +0000294 << Args.getLastArg(OPT_O)->getAsString(Args) << OptLevel;
295 OptLevel = 3;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000296 Success = false;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000297 }
Aaron Ballmane6cc51d2012-02-06 00:40:31 +0000298 Opts.OptimizationLevel = OptLevel;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000299
300 // We must always run at least the always inlining pass.
Douglas Gregor4cdad312012-10-23 20:05:01 +0000301 Opts.setInlining(
302 (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
303 : CodeGenOptions::OnlyAlwaysInlining);
Chad Rosier634a4b12012-03-06 21:17:19 +0000304 // -fno-inline-functions overrides OptimizationLevel > 1.
Chad Rosierfc055f92012-03-15 22:31:42 +0000305 Opts.NoInline = Args.hasArg(OPT_fno_inline);
Douglas Gregor4cdad312012-10-23 20:05:01 +0000306 Opts.setInlining(Args.hasArg(OPT_fno_inline_functions) ?
307 CodeGenOptions::OnlyAlwaysInlining : Opts.getInlining());
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000308
Alexey Samsonovfd00eec2012-05-04 07:39:27 +0000309 if (Args.hasArg(OPT_gline_tables_only)) {
Douglas Gregor4cdad312012-10-23 20:05:01 +0000310 Opts.setDebugInfo(CodeGenOptions::DebugLineTablesOnly);
Alexey Samsonovfd00eec2012-05-04 07:39:27 +0000311 } else if (Args.hasArg(OPT_g_Flag)) {
Alexey Samsonov3a70cd62012-04-27 07:24:20 +0000312 if (Args.hasFlag(OPT_flimit_debug_info, OPT_fno_limit_debug_info, true))
Douglas Gregor4cdad312012-10-23 20:05:01 +0000313 Opts.setDebugInfo(CodeGenOptions::LimitedDebugInfo);
Alexey Samsonov3a70cd62012-04-27 07:24:20 +0000314 else
Douglas Gregor4cdad312012-10-23 20:05:01 +0000315 Opts.setDebugInfo(CodeGenOptions::FullDebugInfo);
Alexey Samsonov3a70cd62012-04-27 07:24:20 +0000316 }
Eric Christopherda3301e2012-10-18 21:52:18 +0000317 Opts.DebugColumnInfo = Args.hasArg(OPT_dwarf_column_info);
Alexey Samsonov3a70cd62012-04-27 07:24:20 +0000318
Douglas Gregorf43b7212013-01-16 01:23:41 +0000319 Opts.ModulesAutolink = Args.hasArg(OPT_fmodules_autolink);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000320 Opts.DisableLLVMOpts = Args.hasArg(OPT_disable_llvm_optzns);
321 Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
John McCall32096692011-03-18 02:56:14 +0000322 Opts.ForbidGuardVariables = Args.hasArg(OPT_fforbid_guard_variables);
Daniel Dunbare26bdb92011-06-21 18:54:46 +0000323 Opts.UseRegisterSizedBitfieldAccess = Args.hasArg(
324 OPT_fuse_register_sized_bitfield_access);
Dan Gohman4d5625e2010-10-14 22:36:56 +0000325 Opts.RelaxedAliasing = Args.hasArg(OPT_relaxed_aliasing);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000326 Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000327 Opts.MergeAllConstants = !Args.hasArg(OPT_fno_merge_all_constants);
328 Opts.NoCommon = Args.hasArg(OPT_fno_common);
329 Opts.NoImplicitFloat = Args.hasArg(OPT_no_implicit_float);
James Molloybfd7a522012-05-01 14:57:16 +0000330 Opts.OptimizeSize = getOptimizationLevelSize(Args, IK, Diags);
Daniel Dunbar6b0cf672010-06-07 23:19:17 +0000331 Opts.SimplifyLibCalls = !(Args.hasArg(OPT_fno_builtin) ||
332 Args.hasArg(OPT_ffreestanding));
Michael J. Spencer20249a12010-10-21 03:16:25 +0000333 Opts.UnrollLoops = Args.hasArg(OPT_funroll_loops) ||
Eric Christopherf84d4092010-08-07 23:08:14 +0000334 (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000335
336 Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose);
John McCallf85e1932011-06-15 23:02:42 +0000337 Opts.ObjCAutoRefCountExceptions = Args.hasArg(OPT_fobjc_arc_exceptions);
Peter Collingbourned51e43a2011-10-06 18:29:46 +0000338 Opts.CUDAIsDevice = Args.hasArg(OPT_fcuda_is_device);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +0000339 Opts.CXAAtExit = !Args.hasArg(OPT_fno_use_cxa_atexit);
340 Opts.CXXCtorDtorAliases = Args.hasArg(OPT_mconstructor_aliases);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000341 Opts.CodeModel = Args.getLastArgValue(OPT_mcode_model);
342 Opts.DebugPass = Args.getLastArgValue(OPT_mdebug_pass);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000343 Opts.DisableFPElim = Args.hasArg(OPT_mdisable_fp_elim);
Nick Lewycky1db772b2012-01-23 08:29:12 +0000344 Opts.DisableTailCalls = Args.hasArg(OPT_mdisable_tail_calls);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000345 Opts.FloatABI = Args.getLastArgValue(OPT_mfloat_abi);
John McCall279b5eb2010-08-12 23:36:15 +0000346 Opts.HiddenWeakVTables = Args.hasArg(OPT_fhidden_weak_vtables);
Peter Collingbourne4400cb82010-12-04 01:51:33 +0000347 Opts.LessPreciseFPMAD = Args.hasArg(OPT_cl_mad_enable);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000348 Opts.LimitFloatPrecision = Args.getLastArgValue(OPT_mlimit_float_precision);
Daniel Dunbar001f6282011-12-09 23:41:18 +0000349 Opts.NoInfsFPMath = (Args.hasArg(OPT_menable_no_infinities) ||
350 Args.hasArg(OPT_cl_finite_math_only)||
351 Args.hasArg(OPT_cl_fast_relaxed_math));
352 Opts.NoNaNsFPMath = (Args.hasArg(OPT_menable_no_nans) ||
353 Args.hasArg(OPT_cl_finite_math_only)||
354 Args.hasArg(OPT_cl_fast_relaxed_math));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000355 Opts.NoZeroInitializedInBSS = Args.hasArg(OPT_mno_zero_initialized_in_bss);
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000356 Opts.BackendOptions = Args.getAllArgValues(OPT_backend_option);
Daniel Dunbar17d3fea2011-02-09 17:54:19 +0000357 Opts.NumRegisterParameters = Args.getLastArgIntValue(OPT_mregparm, 0, Diags);
Chad Rosier1b906052011-08-26 00:26:29 +0000358 Opts.NoGlobalMerge = Args.hasArg(OPT_mno_global_merge);
Nick Lewyckyc3b90142011-06-21 00:14:18 +0000359 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
Daniel Dunbar4ecc9b72010-05-27 05:39:39 +0000360 Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
Daniel Dunbar1ad66482010-07-01 01:31:45 +0000361 Opts.OmitLeafFramePointer = Args.hasArg(OPT_momit_leaf_frame_pointer);
Daniel Dunbar96932322011-03-28 22:49:28 +0000362 Opts.SaveTempLabels = Args.hasArg(OPT_msave_temp_labels);
Rafael Espindolaf24a1512011-04-30 18:35:43 +0000363 Opts.NoDwarf2CFIAsm = Args.hasArg(OPT_fno_dwarf2_cfi_asm);
Nick Lewyckyea523d72011-10-17 23:05:52 +0000364 Opts.NoDwarfDirectoryAsm = Args.hasArg(OPT_fno_dwarf_directory_asm);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000365 Opts.SoftFloat = Args.hasArg(OPT_msoft_float);
Chandler Carruth82fe6ae2012-03-27 23:58:37 +0000366 Opts.StrictEnums = Args.hasArg(OPT_fstrict_enums);
Chandler Carruthabf07a72012-01-02 14:19:45 +0000367 Opts.UnsafeFPMath = Args.hasArg(OPT_menable_unsafe_fp_math) ||
368 Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
Peter Collingbournef0840822010-12-04 01:51:23 +0000369 Args.hasArg(OPT_cl_fast_relaxed_math);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000370 Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000371 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
Bob Wilson71fd6cc2012-02-03 06:27:22 +0000372 Opts.TrapFuncName = Args.getLastArgValue(OPT_ftrap_function_EQ);
Rafael Espindola8af669f2012-06-19 01:26:10 +0000373 Opts.UseInitArray = Args.hasArg(OPT_fuse_init_array);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000374
Chris Lattnerbbea7162010-04-13 00:38:24 +0000375 Opts.FunctionSections = Args.hasArg(OPT_ffunction_sections);
376 Opts.DataSections = Args.hasArg(OPT_fdata_sections);
377
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000378 Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name);
John McCall824e19e2010-02-12 23:47:27 +0000379 Opts.VerifyModule = !Args.hasArg(OPT_disable_llvm_verifier);
Will Dietz2d382d12012-12-30 20:53:28 +0000380 Opts.SanitizeRecover = !Args.hasArg(OPT_fno_sanitize_recover);
Daniel Dunbarf643b9b2010-04-24 17:56:46 +0000381
Chris Lattner7255a2d2010-06-22 00:03:40 +0000382 Opts.InstrumentFunctions = Args.hasArg(OPT_finstrument_functions);
Roman Divackybe4c8702011-02-10 16:52:03 +0000383 Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
Nick Lewyckye8ba8d72011-04-21 23:44:07 +0000384 Opts.EmitGcovArcs = Args.hasArg(OPT_femit_coverage_data);
385 Opts.EmitGcovNotes = Args.hasArg(OPT_femit_coverage_notes);
Tanya Lattner198871c2012-07-11 23:02:10 +0000386 Opts.EmitOpenCLArgMetadata = Args.hasArg(OPT_cl_kernel_arg_info);
Nick Lewycky3dc05412011-05-05 00:08:20 +0000387 Opts.CoverageFile = Args.getLastArgValue(OPT_coverage_file);
Nick Lewycky7c4fd912011-10-21 02:32:14 +0000388 Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);
Peter Collingbourne22a7dfe2011-10-30 17:30:44 +0000389 Opts.LinkBitcodeFile = Args.getLastArgValue(OPT_mlink_bitcode_file);
Alexey Samsonov91ecfa62012-12-03 19:12:58 +0000390 Opts.SanitizerBlacklistFile = Args.getLastArgValue(OPT_fsanitize_blacklist);
Alexey Samsonov4bdc6042013-01-20 13:12:12 +0000391 Opts.SanitizeMemoryTrackOrigins =
Evgeniy Stepanov34ef11b2012-12-24 08:42:34 +0000392 Args.hasArg(OPT_fsanitize_memory_track_origins);
Alexey Samsonov4bdc6042013-01-20 13:12:12 +0000393 Opts.SanitizeAddressZeroBaseShadow =
394 Args.hasArg(OPT_fsanitize_address_zero_base_shadow);
Chad Rosiera7afeb02012-08-21 16:16:06 +0000395 Opts.SSPBufferSize =
396 Args.getLastArgIntValue(OPT_stack_protector_buffer_size, 8, Diags);
Nick Lewycky4e785c92011-12-06 03:33:03 +0000397 Opts.StackRealignment = Args.hasArg(OPT_mstackrealign);
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +0000398 if (Arg *A = Args.getLastArg(OPT_mstack_alignment)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000399 StringRef Val = A->getValue();
Douglas Gregor4cdad312012-10-23 20:05:01 +0000400 unsigned StackAlignment = Opts.StackAlignment;
401 Val.getAsInteger(10, StackAlignment);
402 Opts.StackAlignment = StackAlignment;
Joerg Sonnenbergere9d11db2011-12-05 23:05:23 +0000403 }
Chris Lattner7255a2d2010-06-22 00:03:40 +0000404
Daniel Dunbarf643b9b2010-04-24 17:56:46 +0000405 if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000406 StringRef Name = A->getValue();
Daniel Dunbarf643b9b2010-04-24 17:56:46 +0000407 unsigned Method = llvm::StringSwitch<unsigned>(Name)
408 .Case("legacy", CodeGenOptions::Legacy)
409 .Case("non-legacy", CodeGenOptions::NonLegacy)
410 .Case("mixed", CodeGenOptions::Mixed)
411 .Default(~0U);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000412 if (Method == ~0U) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +0000413 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000414 Success = false;
415 } else {
Douglas Gregor4cdad312012-10-23 20:05:01 +0000416 Opts.setObjCDispatchMethod(
417 static_cast<CodeGenOptions::ObjCDispatchMethodKind>(Method));
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000418 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +0000419 }
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000420
Hans Wennborgde981f32012-06-28 08:01:44 +0000421 if (Arg *A = Args.getLastArg(OPT_ftlsmodel_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000422 StringRef Name = A->getValue();
Hans Wennborgde981f32012-06-28 08:01:44 +0000423 unsigned Model = llvm::StringSwitch<unsigned>(Name)
424 .Case("global-dynamic", CodeGenOptions::GeneralDynamicTLSModel)
425 .Case("local-dynamic", CodeGenOptions::LocalDynamicTLSModel)
426 .Case("initial-exec", CodeGenOptions::InitialExecTLSModel)
427 .Case("local-exec", CodeGenOptions::LocalExecTLSModel)
428 .Default(~0U);
429 if (Model == ~0U) {
430 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
431 Success = false;
432 } else {
Douglas Gregor4cdad312012-10-23 20:05:01 +0000433 Opts.setDefaultTLSModel(static_cast<CodeGenOptions::TLSModel>(Model));
Hans Wennborgde981f32012-06-28 08:01:44 +0000434 }
435 }
436
Lang Hames931c0832012-11-15 07:51:26 +0000437 if (Arg *A = Args.getLastArg(OPT_ffp_contract)) {
438 StringRef Val = A->getValue();
439 if (Val == "fast")
440 Opts.setFPContractMode(CodeGenOptions::FPC_Fast);
441 else if (Val == "on")
442 Opts.setFPContractMode(CodeGenOptions::FPC_On);
443 else if (Val == "off")
444 Opts.setFPContractMode(CodeGenOptions::FPC_Off);
445 else
446 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
447 }
448
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000449 return Success;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000450}
451
452static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
453 ArgList &Args) {
James Molloybfd7a522012-05-01 14:57:16 +0000454 using namespace options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000455 Opts.OutputFile = Args.getLastArgValue(OPT_dependency_file);
456 Opts.Targets = Args.getAllArgValues(OPT_MT);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000457 Opts.IncludeSystemHeaders = Args.hasArg(OPT_sys_header_deps);
458 Opts.UsePhonyTargets = Args.hasArg(OPT_MP);
Daniel Dunbareef63e02011-02-02 15:41:17 +0000459 Opts.ShowHeaderIncludes = Args.hasArg(OPT_H);
Daniel Dunbarb34d69b2011-02-02 21:11:31 +0000460 Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file);
Peter Collingbournebb527862011-07-12 19:35:15 +0000461 Opts.AddMissingHeaderDeps = Args.hasArg(OPT_MG);
Douglas Gregor773303a2012-02-02 23:45:13 +0000462 Opts.DOTOutputFile = Args.getLastArgValue(OPT_dependency_dot);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000463}
464
Chad Rosier2dec85b2012-03-13 20:09:56 +0000465bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
466 DiagnosticsEngine *Diags) {
James Molloybfd7a522012-05-01 14:57:16 +0000467 using namespace options;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000468 bool Success = true;
469
Daniel Dunbar08c66952011-04-07 18:11:14 +0000470 Opts.DiagnosticLogFile = Args.getLastArgValue(OPT_diagnostic_log_file);
Ted Kremenek78002122011-10-29 00:12:39 +0000471 Opts.DiagnosticSerializationFile =
472 Args.getLastArgValue(OPT_diagnostic_serialized_file);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000473 Opts.IgnoreWarnings = Args.hasArg(OPT_w);
474 Opts.NoRewriteMacros = Args.hasArg(OPT_Wno_rewrite_macros);
475 Opts.Pedantic = Args.hasArg(OPT_pedantic);
476 Opts.PedanticErrors = Args.hasArg(OPT_pedantic_errors);
477 Opts.ShowCarets = !Args.hasArg(OPT_fno_caret_diagnostics);
478 Opts.ShowColors = Args.hasArg(OPT_fcolor_diagnostics);
Douglas Gregorc9471b02011-05-21 17:07:29 +0000479 Opts.ShowColumn = Args.hasFlag(OPT_fshow_column,
480 OPT_fno_show_column,
481 /*Default=*/true);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000482 Opts.ShowFixits = !Args.hasArg(OPT_fno_diagnostics_fixit_info);
483 Opts.ShowLocation = !Args.hasArg(OPT_fno_show_source_location);
484 Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option);
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +0000485
Chandler Carruth34b8e682011-03-27 20:00:08 +0000486 // Default behavior is to not to show note include stacks.
487 Opts.ShowNoteIncludeStack = false;
Chandler Carruthabaca7a2011-03-27 01:50:55 +0000488 if (Arg *A = Args.getLastArg(OPT_fdiagnostics_show_note_include_stack,
489 OPT_fno_diagnostics_show_note_include_stack))
Chandler Carruth34b8e682011-03-27 20:00:08 +0000490 if (A->getOption().matches(OPT_fdiagnostics_show_note_include_stack))
491 Opts.ShowNoteIncludeStack = true;
Chandler Carruthabaca7a2011-03-27 01:50:55 +0000492
Chris Lattner5f9e2722011-07-23 10:55:15 +0000493 StringRef ShowOverloads =
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +0000494 Args.getLastArgValue(OPT_fshow_overloads_EQ, "all");
495 if (ShowOverloads == "best")
Douglas Gregordc7b6412012-10-23 23:11:23 +0000496 Opts.setShowOverloads(Ovl_Best);
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +0000497 else if (ShowOverloads == "all")
Douglas Gregordc7b6412012-10-23 23:11:23 +0000498 Opts.setShowOverloads(Ovl_All);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000499 else {
Chad Rosier2dec85b2012-03-13 20:09:56 +0000500 Success = false;
501 if (Diags)
502 Diags->Report(diag::err_drv_invalid_value)
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +0000503 << Args.getLastArg(OPT_fshow_overloads_EQ)->getAsString(Args)
504 << ShowOverloads;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000505 }
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +0000506
Chris Lattner5f9e2722011-07-23 10:55:15 +0000507 StringRef ShowCategory =
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000508 Args.getLastArgValue(OPT_fdiagnostics_show_category, "none");
Chris Lattner6fbe8392010-05-04 21:55:25 +0000509 if (ShowCategory == "none")
510 Opts.ShowCategories = 0;
511 else if (ShowCategory == "id")
512 Opts.ShowCategories = 1;
513 else if (ShowCategory == "name")
514 Opts.ShowCategories = 2;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000515 else {
Chad Rosier2dec85b2012-03-13 20:09:56 +0000516 Success = false;
517 if (Diags)
518 Diags->Report(diag::err_drv_invalid_value)
Chris Lattner6fbe8392010-05-04 21:55:25 +0000519 << Args.getLastArg(OPT_fdiagnostics_show_category)->getAsString(Args)
520 << ShowCategory;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000521 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000522
Chris Lattner5f9e2722011-07-23 10:55:15 +0000523 StringRef Format =
Douglas Gregorc9471b02011-05-21 17:07:29 +0000524 Args.getLastArgValue(OPT_fdiagnostics_format, "clang");
525 if (Format == "clang")
Douglas Gregordc7b6412012-10-23 23:11:23 +0000526 Opts.setFormat(DiagnosticOptions::Clang);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000527 else if (Format == "msvc")
Douglas Gregordc7b6412012-10-23 23:11:23 +0000528 Opts.setFormat(DiagnosticOptions::Msvc);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000529 else if (Format == "vi")
Douglas Gregordc7b6412012-10-23 23:11:23 +0000530 Opts.setFormat(DiagnosticOptions::Vi);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000531 else {
Chad Rosier2dec85b2012-03-13 20:09:56 +0000532 Success = false;
533 if (Diags)
534 Diags->Report(diag::err_drv_invalid_value)
Douglas Gregorc9471b02011-05-21 17:07:29 +0000535 << Args.getLastArg(OPT_fdiagnostics_format)->getAsString(Args)
536 << Format;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000537 }
Douglas Gregorc9471b02011-05-21 17:07:29 +0000538
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000539 Opts.ShowSourceRanges = Args.hasArg(OPT_fdiagnostics_print_source_range_info);
Douglas Gregor4786c152010-08-19 20:24:43 +0000540 Opts.ShowParseableFixits = Args.hasArg(OPT_fdiagnostics_parseable_fixits);
Richard Smith62221b12012-11-14 23:55:25 +0000541 Opts.ShowPresumedLoc = !Args.hasArg(OPT_fno_diagnostics_use_presumed_location);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000542 Opts.VerifyDiagnostics = Args.hasArg(OPT_verify);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000543 Opts.ElideType = !Args.hasArg(OPT_fno_elide_type);
544 Opts.ShowTemplateTree = Args.hasArg(OPT_fdiagnostics_show_template_tree);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000545 Opts.ErrorLimit = Args.getLastArgIntValue(OPT_ferror_limit, 0, Diags);
Douglas Gregor6c1cb992010-05-04 17:13:42 +0000546 Opts.MacroBacktraceLimit
Michael J. Spencer20249a12010-10-21 03:16:25 +0000547 = Args.getLastArgIntValue(OPT_fmacro_backtrace_limit,
Douglas Gregor6c1cb992010-05-04 17:13:42 +0000548 DiagnosticOptions::DefaultMacroBacktraceLimit, Diags);
Douglas Gregor575cf372010-04-20 07:18:24 +0000549 Opts.TemplateBacktraceLimit
Michael J. Spencer20249a12010-10-21 03:16:25 +0000550 = Args.getLastArgIntValue(OPT_ftemplate_backtrace_limit,
551 DiagnosticOptions::DefaultTemplateBacktraceLimit,
Douglas Gregor6c1cb992010-05-04 17:13:42 +0000552 Diags);
Richard Smith08d6e032011-12-16 19:06:07 +0000553 Opts.ConstexprBacktraceLimit
554 = Args.getLastArgIntValue(OPT_fconstexpr_backtrace_limit,
555 DiagnosticOptions::DefaultConstexprBacktraceLimit,
556 Diags);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000557 Opts.TabStop = Args.getLastArgIntValue(OPT_ftabstop,
Chris Lattner52388f92010-01-13 03:06:50 +0000558 DiagnosticOptions::DefaultTabStop, Diags);
559 if (Opts.TabStop == 0 || Opts.TabStop > DiagnosticOptions::MaxTabStop) {
Chris Lattner52388f92010-01-13 03:06:50 +0000560 Opts.TabStop = DiagnosticOptions::DefaultTabStop;
Chad Rosier2dec85b2012-03-13 20:09:56 +0000561 if (Diags)
562 Diags->Report(diag::warn_ignoring_ftabstop_value)
563 << Opts.TabStop << DiagnosticOptions::DefaultTabStop;
Chris Lattner52388f92010-01-13 03:06:50 +0000564 }
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000565 Opts.MessageLength = Args.getLastArgIntValue(OPT_fmessage_length, 0, Diags);
James Molloybfd7a522012-05-01 14:57:16 +0000566 addWarningArgs(Args, Opts.Warnings);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000567
568 return Success;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000569}
570
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000571static void ParseFileSystemArgs(FileSystemOptions &Opts, ArgList &Args) {
572 Opts.WorkingDir = Args.getLastArgValue(OPT_working_directory);
573}
574
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000575static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
David Blaikied6471f72011-09-25 23:23:43 +0000576 DiagnosticsEngine &Diags) {
James Molloybfd7a522012-05-01 14:57:16 +0000577 using namespace options;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000578 Opts.ProgramAction = frontend::ParseSyntaxOnly;
579 if (const Arg *A = Args.getLastArg(OPT_Action_Group)) {
580 switch (A->getOption().getID()) {
581 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000582 llvm_unreachable("Invalid option in group!");
Alexander Kornienko171af642012-07-31 09:37:40 +0000583 case OPT_ast_list:
584 Opts.ProgramAction = frontend::ASTDeclList; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000585 case OPT_ast_dump:
586 Opts.ProgramAction = frontend::ASTDump; break;
John McCallf3514242010-11-24 11:21:45 +0000587 case OPT_ast_dump_xml:
588 Opts.ProgramAction = frontend::ASTDumpXML; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000589 case OPT_ast_print:
590 Opts.ProgramAction = frontend::ASTPrint; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000591 case OPT_ast_view:
592 Opts.ProgramAction = frontend::ASTView; break;
593 case OPT_dump_raw_tokens:
594 Opts.ProgramAction = frontend::DumpRawTokens; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000595 case OPT_dump_tokens:
596 Opts.ProgramAction = frontend::DumpTokens; break;
597 case OPT_S:
598 Opts.ProgramAction = frontend::EmitAssembly; break;
599 case OPT_emit_llvm_bc:
600 Opts.ProgramAction = frontend::EmitBC; break;
601 case OPT_emit_html:
602 Opts.ProgramAction = frontend::EmitHTML; break;
603 case OPT_emit_llvm:
604 Opts.ProgramAction = frontend::EmitLLVM; break;
605 case OPT_emit_llvm_only:
606 Opts.ProgramAction = frontend::EmitLLVMOnly; break;
Daniel Dunbar32148ce2010-05-25 18:41:01 +0000607 case OPT_emit_codegen_only:
608 Opts.ProgramAction = frontend::EmitCodeGenOnly; break;
Daniel Dunbarda1573f2010-02-03 01:18:43 +0000609 case OPT_emit_obj:
610 Opts.ProgramAction = frontend::EmitObj; break;
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000611 case OPT_fixit_EQ:
Richard Smith1d489cf2012-11-01 04:30:05 +0000612 Opts.FixItSuffix = A->getValue();
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000613 // fall-through!
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000614 case OPT_fixit:
615 Opts.ProgramAction = frontend::FixIt; break;
Douglas Gregor467dc882011-08-25 22:30:56 +0000616 case OPT_emit_module:
617 Opts.ProgramAction = frontend::GenerateModule; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000618 case OPT_emit_pch:
619 Opts.ProgramAction = frontend::GeneratePCH; break;
620 case OPT_emit_pth:
621 Opts.ProgramAction = frontend::GeneratePTH; break;
Daniel Dunbar27585952010-03-19 19:44:04 +0000622 case OPT_init_only:
623 Opts.ProgramAction = frontend::InitOnly; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000624 case OPT_fsyntax_only:
625 Opts.ProgramAction = frontend::ParseSyntaxOnly; break;
626 case OPT_print_decl_contexts:
627 Opts.ProgramAction = frontend::PrintDeclContext; break;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000628 case OPT_print_preamble:
629 Opts.ProgramAction = frontend::PrintPreamble; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000630 case OPT_E:
631 Opts.ProgramAction = frontend::PrintPreprocessedInput; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000632 case OPT_rewrite_macros:
633 Opts.ProgramAction = frontend::RewriteMacros; break;
634 case OPT_rewrite_objc:
635 Opts.ProgramAction = frontend::RewriteObjC; break;
636 case OPT_rewrite_test:
637 Opts.ProgramAction = frontend::RewriteTest; break;
638 case OPT_analyze:
639 Opts.ProgramAction = frontend::RunAnalysis; break;
Ted Kremenek30660a82012-03-06 20:06:33 +0000640 case OPT_migrate:
641 Opts.ProgramAction = frontend::MigrateSource; break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000642 case OPT_Eonly:
643 Opts.ProgramAction = frontend::RunPreprocessorOnly; break;
644 }
645 }
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000646
647 if (const Arg* A = Args.getLastArg(OPT_plugin)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000648 Opts.Plugins.push_back(A->getValue(0));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000649 Opts.ProgramAction = frontend::PluginAction;
Richard Smith1d489cf2012-11-01 04:30:05 +0000650 Opts.ActionName = A->getValue();
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000651
652 for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg),
653 end = Args.filtered_end(); it != end; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000654 if ((*it)->getValue(0) == Opts.ActionName)
655 Opts.PluginArgs.push_back((*it)->getValue(1));
Daniel Dunbar3177aae2010-06-16 16:59:23 +0000656 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000657 }
658
Nico Weber5aa74af2011-01-25 20:34:14 +0000659 Opts.AddPluginActions = Args.getAllArgValues(OPT_add_plugin);
Nico Weberf25649c2011-01-29 21:21:49 +0000660 Opts.AddPluginArgs.resize(Opts.AddPluginActions.size());
661 for (int i = 0, e = Opts.AddPluginActions.size(); i != e; ++i) {
662 for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg),
663 end = Args.filtered_end(); it != end; ++it) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000664 if ((*it)->getValue(0) == Opts.AddPluginActions[i])
665 Opts.AddPluginArgs[i].push_back((*it)->getValue(1));
Nico Weberf25649c2011-01-29 21:21:49 +0000666 }
667 }
Nico Weber5aa74af2011-01-25 20:34:14 +0000668
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000669 if (const Arg *A = Args.getLastArg(OPT_code_completion_at)) {
670 Opts.CodeCompletionAt =
Richard Smith1d489cf2012-11-01 04:30:05 +0000671 ParsedSourceLocation::FromString(A->getValue());
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000672 if (Opts.CodeCompletionAt.FileName.empty())
673 Diags.Report(diag::err_drv_invalid_value)
Richard Smith1d489cf2012-11-01 04:30:05 +0000674 << A->getAsString(Args) << A->getValue();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000675 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000676 Opts.DisableFree = Args.hasArg(OPT_disable_free);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000677
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000678 Opts.OutputFile = Args.getLastArgValue(OPT_o);
679 Opts.Plugins = Args.getAllArgValues(OPT_load);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000680 Opts.RelocatablePCH = Args.hasArg(OPT_relocatable_pch);
Daniel Dunbar9b5e9ae2009-12-03 07:01:58 +0000681 Opts.ShowHelp = Args.hasArg(OPT_help);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000682 Opts.ShowStats = Args.hasArg(OPT_print_stats);
683 Opts.ShowTimers = Args.hasArg(OPT_ftime_report);
Daniel Dunbar9b5e9ae2009-12-03 07:01:58 +0000684 Opts.ShowVersion = Args.hasArg(OPT_version);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000685 Opts.ASTMergeFiles = Args.getAllArgValues(OPT_ast_merge);
686 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
Nick Lewycky1450f262010-08-13 17:31:00 +0000687 Opts.FixWhatYouCan = Args.hasArg(OPT_fix_what_you_can);
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000688 Opts.FixOnlyWarnings = Args.hasArg(OPT_fix_only_warnings);
689 Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile);
690 Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp);
Alexander Kornienkoe34a0522012-07-26 16:01:23 +0000691 Opts.ASTDumpFilter = Args.getLastArgValue(OPT_ast_dump_filter);
Douglas Gregor20c6f2e2013-01-28 18:38:02 +0000692 Opts.UseGlobalModuleIndex = !Args.hasArg(OPT_fno_modules_global_index);
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000693 Opts.GenerateGlobalModuleIndex = Opts.UseGlobalModuleIndex;
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000694
Dmitri Gribenkod99ef532012-07-02 17:35:10 +0000695 Opts.CodeCompleteOpts.IncludeMacros
696 = Args.hasArg(OPT_code_completion_macros);
697 Opts.CodeCompleteOpts.IncludeCodePatterns
698 = Args.hasArg(OPT_code_completion_patterns);
699 Opts.CodeCompleteOpts.IncludeGlobals
700 = !Args.hasArg(OPT_no_code_completion_globals);
701 Opts.CodeCompleteOpts.IncludeBriefComments
702 = Args.hasArg(OPT_code_completion_brief_comments);
703
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000704 Opts.OverrideRecordLayoutsFile
705 = Args.getLastArgValue(OPT_foverride_record_layout_EQ);
John McCall8f0e8d22011-06-15 23:25:17 +0000706 if (const Arg *A = Args.getLastArg(OPT_arcmt_check,
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000707 OPT_arcmt_modify,
708 OPT_arcmt_migrate)) {
John McCall8f0e8d22011-06-15 23:25:17 +0000709 switch (A->getOption().getID()) {
710 default:
711 llvm_unreachable("missed a case");
712 case OPT_arcmt_check:
713 Opts.ARCMTAction = FrontendOptions::ARCMT_Check;
714 break;
715 case OPT_arcmt_modify:
716 Opts.ARCMTAction = FrontendOptions::ARCMT_Modify;
717 break;
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000718 case OPT_arcmt_migrate:
719 Opts.ARCMTAction = FrontendOptions::ARCMT_Migrate;
720 break;
John McCall8f0e8d22011-06-15 23:25:17 +0000721 }
722 }
Ted Kremenek30660a82012-03-06 20:06:33 +0000723 Opts.MTMigrateDir = Args.getLastArgValue(OPT_mt_migrate_directory);
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +0000724 Opts.ARCMTMigrateReportOut
725 = Args.getLastArgValue(OPT_arcmt_migrate_report_output);
726 Opts.ARCMTMigrateEmitARCErrors
727 = Args.hasArg(OPT_arcmt_migrate_emit_arc_errors);
John McCall8f0e8d22011-06-15 23:25:17 +0000728
Ted Kremenek30660a82012-03-06 20:06:33 +0000729 if (Args.hasArg(OPT_objcmt_migrate_literals))
730 Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Literals;
731 if (Args.hasArg(OPT_objcmt_migrate_subscripting))
732 Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Subscripting;
733
734 if (Opts.ARCMTAction != FrontendOptions::ARCMT_None &&
735 Opts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
736 Diags.Report(diag::err_drv_argument_not_allowed_with)
737 << "ARC migration" << "ObjC migration";
738 }
739
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000740 InputKind DashX = IK_None;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000741 if (const Arg *A = Args.getLastArg(OPT_x)) {
Richard Smith1d489cf2012-11-01 04:30:05 +0000742 DashX = llvm::StringSwitch<InputKind>(A->getValue())
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000743 .Case("c", IK_C)
744 .Case("cl", IK_OpenCL)
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000745 .Case("cuda", IK_CUDA)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000746 .Case("c++", IK_CXX)
747 .Case("objective-c", IK_ObjC)
748 .Case("objective-c++", IK_ObjCXX)
749 .Case("cpp-output", IK_PreprocessedC)
750 .Case("assembler-with-cpp", IK_Asm)
751 .Case("c++-cpp-output", IK_PreprocessedCXX)
752 .Case("objective-c-cpp-output", IK_PreprocessedObjC)
Chris Lattner919f2ca2011-04-09 07:09:31 +0000753 .Case("objc-cpp-output", IK_PreprocessedObjC)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000754 .Case("objective-c++-cpp-output", IK_PreprocessedObjCXX)
Nico Weber1f7957d2011-08-13 19:03:50 +0000755 .Case("objc++-cpp-output", IK_PreprocessedObjCXX)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000756 .Case("c-header", IK_C)
Peter Collingbourneae57ead2011-10-09 22:03:19 +0000757 .Case("cl-header", IK_OpenCL)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000758 .Case("objective-c-header", IK_ObjC)
759 .Case("c++-header", IK_CXX)
760 .Case("objective-c++-header", IK_ObjCXX)
761 .Case("ast", IK_AST)
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000762 .Case("ir", IK_LLVM_IR)
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000763 .Default(IK_None);
764 if (DashX == IK_None)
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000765 Diags.Report(diag::err_drv_invalid_value)
Richard Smith1d489cf2012-11-01 04:30:05 +0000766 << A->getAsString(Args) << A->getValue();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000767 }
768
769 // '-' is the default input if none is given.
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000770 std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000771 Opts.Inputs.clear();
772 if (Inputs.empty())
773 Inputs.push_back("-");
774 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000775 InputKind IK = DashX;
776 if (IK == IK_None) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000777 IK = FrontendOptions::getInputKindForExtension(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000778 StringRef(Inputs[i]).rsplit('.').second);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000779 // FIXME: Remove this hack.
780 if (i == 0)
781 DashX = IK;
782 }
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000783 Opts.Inputs.push_back(FrontendInputFile(Inputs[i], IK));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000784 }
785
786 return DashX;
787}
788
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +0000789std::string CompilerInvocation::GetResourcesPath(const char *Argv0,
790 void *MainAddr) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000791 llvm::sys::Path P = llvm::sys::Path::GetMainExecutable(Argv0, MainAddr);
792
793 if (!P.isEmpty()) {
794 P.eraseComponent(); // Remove /clang from foo/bin/clang
795 P.eraseComponent(); // Remove /bin from foo/bin
796
797 // Get foo/lib/clang/<version>/include
798 P.appendComponent("lib");
799 P.appendComponent("clang");
800 P.appendComponent(CLANG_VERSION_STRING);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000801 }
802
803 return P.str();
804}
805
Daniel Dunbar1e69fe32009-12-13 03:45:58 +0000806static void ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args) {
James Molloybfd7a522012-05-01 14:57:16 +0000807 using namespace options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000808 Opts.Sysroot = Args.getLastArgValue(OPT_isysroot, "/");
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000809 Opts.Verbose = Args.hasArg(OPT_v);
Francois Pichetc91562c2011-10-10 20:34:10 +0000810 Opts.UseBuiltinIncludes = !Args.hasArg(OPT_nobuiltininc);
Daniel Dunbara268fc02011-10-11 18:20:10 +0000811 Opts.UseStandardSystemIncludes = !Args.hasArg(OPT_nostdsysteminc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +0000812 Opts.UseStandardCXXIncludes = !Args.hasArg(OPT_nostdincxx);
Bob Wilson13c4f212011-06-21 21:12:29 +0000813 if (const Arg *A = Args.getLastArg(OPT_stdlib_EQ))
Richard Smith1d489cf2012-11-01 04:30:05 +0000814 Opts.UseLibcxx = (strcmp(A->getValue(), "libc++") == 0);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +0000815 Opts.ResourceDir = Args.getLastArgValue(OPT_resource_dir);
Douglas Gregor9a6da692011-09-12 20:41:59 +0000816 Opts.ModuleCachePath = Args.getLastArgValue(OPT_fmodule_cache_path);
Douglas Gregor6e975c42011-09-13 23:15:45 +0000817 Opts.DisableModuleHash = Args.hasArg(OPT_fdisable_module_hash);
Douglas Gregor9a6da692011-09-12 20:41:59 +0000818
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000819 // Add -I..., -F..., and -index-header-map options in order.
820 bool IsIndexHeaderMap = false;
821 for (arg_iterator it = Args.filtered_begin(OPT_I, OPT_F,
822 OPT_index_header_map),
823 ie = Args.filtered_end(); it != ie; ++it) {
824 if ((*it)->getOption().matches(OPT_index_header_map)) {
825 // -index-header-map applies to the next -I or -F.
826 IsIndexHeaderMap = true;
827 continue;
828 }
829
830 frontend::IncludeDirGroup Group
831 = IsIndexHeaderMap? frontend::IndexHeaderMap : frontend::Angled;
832
Daniel Dunbareab66522013-01-25 01:50:47 +0000833 Opts.AddPath((*it)->getValue(), Group,
Bob Wilson5dd45f12011-06-21 21:53:08 +0000834 /*IsFramework=*/ (*it)->getOption().matches(OPT_F), false);
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000835 IsIndexHeaderMap = false;
836 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000837
Daniel Dunbarf3fc21f2013-01-25 01:50:34 +0000838 // Add -iprefix/-iwithprefix/-iwithprefixbefore options.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000839 StringRef Prefix = ""; // FIXME: This isn't the correct default prefix.
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000840 for (arg_iterator it = Args.filtered_begin(OPT_iprefix, OPT_iwithprefix,
841 OPT_iwithprefixbefore),
842 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000843 const Arg *A = *it;
844 if (A->getOption().matches(OPT_iprefix))
Richard Smith1d489cf2012-11-01 04:30:05 +0000845 Prefix = A->getValue();
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000846 else if (A->getOption().matches(OPT_iwithprefix))
Richard Smith1d489cf2012-11-01 04:30:05 +0000847 Opts.AddPath(Prefix.str() + A->getValue(),
Daniel Dunbareab66522013-01-25 01:50:47 +0000848 frontend::After, false, false);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000849 else
Richard Smith1d489cf2012-11-01 04:30:05 +0000850 Opts.AddPath(Prefix.str() + A->getValue(),
Daniel Dunbareab66522013-01-25 01:50:47 +0000851 frontend::Angled, false, false);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000852 }
853
854 for (arg_iterator it = Args.filtered_begin(OPT_idirafter),
855 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000856 Opts.AddPath((*it)->getValue(), frontend::After, false, false);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000857 for (arg_iterator it = Args.filtered_begin(OPT_iquote),
858 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000859 Opts.AddPath((*it)->getValue(), frontend::Quoted, false, false);
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000860 for (arg_iterator it = Args.filtered_begin(OPT_isystem,
Joerg Sonnenberger2df66472011-02-22 00:40:56 +0000861 OPT_iwithsysroot), ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000862 Opts.AddPath((*it)->getValue(), frontend::System, false,
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000863 !(*it)->getOption().matches(OPT_iwithsysroot));
Daniel Dunbar024f4be2011-10-18 20:40:38 +0000864 for (arg_iterator it = Args.filtered_begin(OPT_iframework),
865 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000866 Opts.AddPath((*it)->getValue(), frontend::System, true, true);
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000867
868 // Add the paths for the various language specific isystem flags.
869 for (arg_iterator it = Args.filtered_begin(OPT_c_isystem),
870 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000871 Opts.AddPath((*it)->getValue(), frontend::CSystem, false, true);
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000872 for (arg_iterator it = Args.filtered_begin(OPT_cxx_isystem),
873 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000874 Opts.AddPath((*it)->getValue(), frontend::CXXSystem, false, true);
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000875 for (arg_iterator it = Args.filtered_begin(OPT_objc_isystem),
876 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000877 Opts.AddPath((*it)->getValue(), frontend::ObjCSystem, false,true);
Benjamin Kramer47adebe2011-09-22 21:41:16 +0000878 for (arg_iterator it = Args.filtered_begin(OPT_objcxx_isystem),
879 ie = Args.filtered_end(); it != ie; ++it)
Daniel Dunbareab66522013-01-25 01:50:47 +0000880 Opts.AddPath((*it)->getValue(), frontend::ObjCXXSystem, false, true);
Chandler Carruthac2bc4d2011-11-05 08:30:29 +0000881
882 // Add the internal paths from a driver that detects standard include paths.
883 for (arg_iterator I = Args.filtered_begin(OPT_internal_isystem,
Chandler Carruth07643082011-11-07 09:17:31 +0000884 OPT_internal_externc_isystem),
Chandler Carruthac2bc4d2011-11-05 08:30:29 +0000885 E = Args.filtered_end();
886 I != E; ++I)
Richard Smith1d489cf2012-11-01 04:30:05 +0000887 Opts.AddPath((*I)->getValue(), frontend::System,
Daniel Dunbareab66522013-01-25 01:50:47 +0000888 false, /*IgnoreSysRoot=*/true, /*IsInternal=*/true,
Chandler Carruthac2bc4d2011-11-05 08:30:29 +0000889 (*I)->getOption().matches(OPT_internal_externc_isystem));
Richard Smithf122a132012-06-13 20:27:03 +0000890
891 // Add the path prefixes which are implicitly treated as being system headers.
892 for (arg_iterator I = Args.filtered_begin(OPT_isystem_prefix,
893 OPT_ino_system_prefix),
894 E = Args.filtered_end();
895 I != E; ++I)
Richard Smith1d489cf2012-11-01 04:30:05 +0000896 Opts.AddSystemHeaderPrefix((*I)->getValue(),
Richard Smithf122a132012-06-13 20:27:03 +0000897 (*I)->getOption().matches(OPT_isystem_prefix));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000898}
899
Peter Collingbourne53c92be2010-12-04 01:50:27 +0000900void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,
901 LangStandard::Kind LangStd) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000902 // Set some properties which depend solely on the input kind; it would be nice
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000903 // to move these to the language standard, and have the driver resolve the
904 // input kind + language standard.
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000905 if (IK == IK_Asm) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000906 Opts.AsmPreprocessor = 1;
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000907 } else if (IK == IK_ObjC ||
908 IK == IK_ObjCXX ||
909 IK == IK_PreprocessedObjC ||
910 IK == IK_PreprocessedObjCXX) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000911 Opts.ObjC1 = Opts.ObjC2 = 1;
912 }
913
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000914 if (LangStd == LangStandard::lang_unspecified) {
915 // Based on the base language, pick one.
916 switch (IK) {
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000917 case IK_None:
918 case IK_AST:
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000919 case IK_LLVM_IR:
David Blaikieb219cfc2011-09-23 05:06:16 +0000920 llvm_unreachable("Invalid input kind!");
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000921 case IK_OpenCL:
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000922 LangStd = LangStandard::lang_opencl;
923 break;
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000924 case IK_CUDA:
925 LangStd = LangStandard::lang_cuda;
926 break;
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000927 case IK_Asm:
928 case IK_C:
929 case IK_PreprocessedC:
930 case IK_ObjC:
931 case IK_PreprocessedObjC:
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000932 LangStd = LangStandard::lang_gnu99;
933 break;
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +0000934 case IK_CXX:
935 case IK_PreprocessedCXX:
936 case IK_ObjCXX:
937 case IK_PreprocessedObjCXX:
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000938 LangStd = LangStandard::lang_gnucxx98;
939 break;
940 }
941 }
942
943 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Nico Weberbb236282012-11-11 07:02:14 +0000944 Opts.LineComment = Std.hasLineComments();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000945 Opts.C99 = Std.isC99();
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000946 Opts.C11 = Std.isC11();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000947 Opts.CPlusPlus = Std.isCPlusPlus();
Richard Smith80ad52f2013-01-02 11:42:31 +0000948 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Richard Smith5ba49c02012-10-17 23:07:52 +0000949 Opts.CPlusPlus1y = Std.isCPlusPlus1y();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000950 Opts.Digraphs = Std.hasDigraphs();
951 Opts.GNUMode = Std.isGNUMode();
952 Opts.GNUInline = !Std.isC99();
953 Opts.HexFloats = Std.hasHexFloats();
954 Opts.ImplicitInt = Std.hasImplicitInt();
955
Tanya Lattnerf4205e12012-06-18 22:55:02 +0000956 // Set OpenCL Version.
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000957 if (LangStd == LangStandard::lang_opencl) {
958 Opts.OpenCL = 1;
Tanya Lattnerf4205e12012-06-18 22:55:02 +0000959 Opts.OpenCLVersion = 100;
960 }
961 else if (LangStd == LangStandard::lang_opencl11) {
962 Opts.OpenCL = 1;
963 Opts.OpenCLVersion = 110;
964 }
965 else if (LangStd == LangStandard::lang_opencl12) {
966 Opts.OpenCL = 1;
967 Opts.OpenCLVersion = 120;
968 }
969
970 // OpenCL has some additional defaults.
971 if (Opts.OpenCL) {
Tobias Grosser37c31c22011-09-21 18:28:29 +0000972 Opts.AltiVec = 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000973 Opts.CXXOperatorNames = 1;
Tobias Grosser37c31c22011-09-21 18:28:29 +0000974 Opts.LaxVectorConversions = 0;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000975 Opts.DefaultFPContract = 1;
Joey Gouly19dbb202013-01-23 11:56:20 +0000976 Opts.NativeHalfType = 1;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000977 }
978
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000979 if (LangStd == LangStandard::lang_cuda)
980 Opts.CUDA = 1;
981
Daniel Dunbar56ac85c2009-12-01 03:16:53 +0000982 // OpenCL and C++ both have bool, true, false keywords.
983 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
984
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000985 // C++ has wchar_t keyword.
986 Opts.WChar = Opts.CPlusPlus;
987
Peter Collingbourne53c92be2010-12-04 01:50:27 +0000988 Opts.GNUKeywords = Opts.GNUMode;
989 Opts.CXXOperatorNames = Opts.CPlusPlus;
990
991 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
992 // is specified, or -std is set to a conforming mode.
993 Opts.Trigraphs = !Opts.GNUMode;
994
995 Opts.DollarIdents = !Opts.AsmPreprocessor;
996}
997
998static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
David Blaikied6471f72011-09-25 23:23:43 +0000999 DiagnosticsEngine &Diags) {
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001000 // FIXME: Cleanup per-file based stuff.
1001 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
1002 if (const Arg *A = Args.getLastArg(OPT_std_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001003 LangStd = llvm::StringSwitch<LangStandard::Kind>(A->getValue())
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001004#define LANGSTANDARD(id, name, desc, features) \
1005 .Case(name, LangStandard::lang_##id)
1006#include "clang/Frontend/LangStandards.def"
1007 .Default(LangStandard::lang_unspecified);
1008 if (LangStd == LangStandard::lang_unspecified)
1009 Diags.Report(diag::err_drv_invalid_value)
Richard Smith1d489cf2012-11-01 04:30:05 +00001010 << A->getAsString(Args) << A->getValue();
Chad Rosierb2dba4b2011-05-02 19:24:53 +00001011 else {
1012 // Valid standard, check to make sure language and standard are compatable.
1013 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
1014 switch (IK) {
1015 case IK_C:
1016 case IK_ObjC:
1017 case IK_PreprocessedC:
1018 case IK_PreprocessedObjC:
1019 if (!(Std.isC89() || Std.isC99()))
1020 Diags.Report(diag::err_drv_argument_not_allowed_with)
1021 << A->getAsString(Args) << "C/ObjC";
1022 break;
1023 case IK_CXX:
1024 case IK_ObjCXX:
1025 case IK_PreprocessedCXX:
1026 case IK_PreprocessedObjCXX:
1027 if (!Std.isCPlusPlus())
1028 Diags.Report(diag::err_drv_argument_not_allowed_with)
1029 << A->getAsString(Args) << "C++/ObjC++";
1030 break;
1031 case IK_OpenCL:
1032 if (!Std.isC99())
1033 Diags.Report(diag::err_drv_argument_not_allowed_with)
1034 << A->getAsString(Args) << "OpenCL";
1035 break;
1036 case IK_CUDA:
1037 if (!Std.isCPlusPlus())
1038 Diags.Report(diag::err_drv_argument_not_allowed_with)
1039 << A->getAsString(Args) << "CUDA";
1040 break;
1041 default:
1042 break;
1043 }
1044 }
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001045 }
1046
Tanya Lattner5e94d6f2012-06-19 23:09:52 +00001047 // -cl-std only applies for OpenCL language standards.
1048 // Override the -std option in this case.
Peter Collingbourne91252d12010-12-04 01:51:40 +00001049 if (const Arg *A = Args.getLastArg(OPT_cl_std_EQ)) {
Tanya Lattner5e94d6f2012-06-19 23:09:52 +00001050 LangStandard::Kind OpenCLLangStd
Richard Smith1d489cf2012-11-01 04:30:05 +00001051 = llvm::StringSwitch<LangStandard::Kind>(A->getValue())
Tanya Lattner5e94d6f2012-06-19 23:09:52 +00001052 .Case("CL", LangStandard::lang_opencl)
1053 .Case("CL1.1", LangStandard::lang_opencl11)
1054 .Case("CL1.2", LangStandard::lang_opencl12)
1055 .Default(LangStandard::lang_unspecified);
1056
1057 if (OpenCLLangStd == LangStandard::lang_unspecified) {
Peter Collingbourne91252d12010-12-04 01:51:40 +00001058 Diags.Report(diag::err_drv_invalid_value)
Richard Smith1d489cf2012-11-01 04:30:05 +00001059 << A->getAsString(Args) << A->getValue();
Peter Collingbourne91252d12010-12-04 01:51:40 +00001060 }
Tanya Lattner5e94d6f2012-06-19 23:09:52 +00001061 else
1062 LangStd = OpenCLLangStd;
Peter Collingbourne91252d12010-12-04 01:51:40 +00001063 }
Tanya Lattner5e94d6f2012-06-19 23:09:52 +00001064
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001065 CompilerInvocation::setLangDefaults(Opts, IK, LangStd);
1066
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001067 // We abuse '-f[no-]gnu-keywords' to force overriding all GNU-extension
1068 // keywords. This behavior is provided by GCC's poorly named '-fasm' flag,
1069 // while a subset (the non-C++ GNU keywords) is provided by GCC's
1070 // '-fgnu-keywords'. Clang conflates the two for simplicity under the single
1071 // name, as it doesn't seem a useful distinction.
1072 Opts.GNUKeywords = Args.hasFlag(OPT_fgnu_keywords, OPT_fno_gnu_keywords,
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001073 Opts.GNUKeywords);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001074
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001075 if (Args.hasArg(OPT_fno_operator_names))
1076 Opts.CXXOperatorNames = 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001077
John McCallf85e1932011-06-15 23:02:42 +00001078 if (Opts.ObjC1) {
John McCall260611a2012-06-20 06:18:46 +00001079 if (Arg *arg = Args.getLastArg(OPT_fobjc_runtime_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001080 StringRef value = arg->getValue();
John McCall260611a2012-06-20 06:18:46 +00001081 if (Opts.ObjCRuntime.tryParse(value))
1082 Diags.Report(diag::err_drv_unknown_objc_runtime) << value;
1083 }
1084
John McCallf85e1932011-06-15 23:02:42 +00001085 if (Args.hasArg(OPT_fobjc_gc_only))
Douglas Gregore289d812011-09-13 17:21:33 +00001086 Opts.setGC(LangOptions::GCOnly);
John McCallf85e1932011-06-15 23:02:42 +00001087 else if (Args.hasArg(OPT_fobjc_gc))
Douglas Gregore289d812011-09-13 17:21:33 +00001088 Opts.setGC(LangOptions::HybridGC);
John McCallf85e1932011-06-15 23:02:42 +00001089 else if (Args.hasArg(OPT_fobjc_arc)) {
1090 Opts.ObjCAutoRefCount = 1;
John McCall0a7dd782012-08-21 02:47:43 +00001091 if (!Opts.ObjCRuntime.allowsARC())
1092 Diags.Report(diag::err_arc_unsupported_on_runtime);
John McCallf85e1932011-06-15 23:02:42 +00001093
John McCall0a7dd782012-08-21 02:47:43 +00001094 // Only set ObjCARCWeak if ARC is enabled.
1095 if (Args.hasArg(OPT_fobjc_runtime_has_weak))
1096 Opts.ObjCARCWeak = 1;
1097 else
1098 Opts.ObjCARCWeak = Opts.ObjCRuntime.allowsWeak();
1099 }
John McCall9f084a32011-07-06 00:26:06 +00001100
John McCallf85e1932011-06-15 23:02:42 +00001101 if (Args.hasArg(OPT_fno_objc_infer_related_result_type))
1102 Opts.ObjCInferRelatedResultType = 0;
1103 }
1104
Rafael Espindolafb3f4aa2011-06-02 16:13:27 +00001105 if (Args.hasArg(OPT_fgnu89_inline))
1106 Opts.GNUInline = 1;
1107
Fariborz Jahanian142f9e92011-02-04 00:01:24 +00001108 if (Args.hasArg(OPT_fapple_kext)) {
1109 if (!Opts.CPlusPlus)
1110 Diags.Report(diag::warn_c_kext);
1111 else
1112 Opts.AppleKext = 1;
1113 }
1114
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001115 if (Args.hasArg(OPT_print_ivar_layout))
1116 Opts.ObjCGCBitmapPrint = 1;
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001117 if (Args.hasArg(OPT_fno_constant_cfstrings))
1118 Opts.NoConstantCFStrings = 1;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001119
1120 if (Args.hasArg(OPT_faltivec))
1121 Opts.AltiVec = 1;
1122
1123 if (Args.hasArg(OPT_pthread))
1124 Opts.POSIXThreads = 1;
1125
Francois Pichet8387e2a2011-04-22 22:18:13 +00001126 if (Args.hasArg(OPT_fdelayed_template_parsing))
1127 Opts.DelayedTemplateParsing = 1;
1128
Chris Lattner5f9e2722011-07-23 10:55:15 +00001129 StringRef Vis = Args.getLastArgValue(OPT_fvisibility, "default");
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001130 if (Vis == "default")
John McCall1fb0caa2010-10-22 21:05:15 +00001131 Opts.setVisibilityMode(DefaultVisibility);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001132 else if (Vis == "hidden")
John McCall1fb0caa2010-10-22 21:05:15 +00001133 Opts.setVisibilityMode(HiddenVisibility);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001134 else if (Vis == "protected")
John McCall41887602012-01-29 01:20:30 +00001135 // FIXME: diagnose if target does not support protected visibility
John McCall1fb0caa2010-10-22 21:05:15 +00001136 Opts.setVisibilityMode(ProtectedVisibility);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001137 else
1138 Diags.Report(diag::err_drv_invalid_value)
1139 << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
1140
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001141 if (Args.hasArg(OPT_fvisibility_inlines_hidden))
1142 Opts.InlineVisibilityHidden = 1;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001143
David Chisnall7f18e672010-09-17 18:29:54 +00001144 if (Args.hasArg(OPT_ftrapv)) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00001145 Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping);
David Chisnall7f18e672010-09-17 18:29:54 +00001146 // Set the handler, if one is specified.
1147 Opts.OverflowHandler =
1148 Args.getLastArgValue(OPT_ftrapv_handler);
1149 }
Chris Lattnera4d71452010-06-26 21:25:03 +00001150 else if (Args.hasArg(OPT_fwrapv))
Michael J. Spencer20249a12010-10-21 03:16:25 +00001151 Opts.setSignedOverflowBehavior(LangOptions::SOB_Defined);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001152
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001153 if (Args.hasArg(OPT_trigraphs))
1154 Opts.Trigraphs = 1;
1155
Daniel Dunbar8663b182009-12-16 20:10:18 +00001156 Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
1157 OPT_fno_dollars_in_identifiers,
Peter Collingbourne53c92be2010-12-04 01:50:27 +00001158 Opts.DollarIdents);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001159 Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
Douglas Gregor0bb935c2011-10-24 15:27:23 +00001160 Opts.MicrosoftExt
1161 = Args.hasArg(OPT_fms_extensions) || Args.hasArg(OPT_fms_compatibility);
Francois Pichetae556082011-09-17 04:32:15 +00001162 Opts.MicrosoftMode = Args.hasArg(OPT_fms_compatibility);
Chad Rosier4fb10d12012-12-05 23:35:10 +00001163 Opts.AsmBlocks = Args.hasArg(OPT_fasm_blocks) || Opts.MicrosoftExt;
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001164 Opts.MSCVersion = Args.getLastArgIntValue(OPT_fmsc_version, 0, Diags);
Dawn Perchik400b6072010-09-02 23:59:25 +00001165 Opts.Borland = Args.hasArg(OPT_fborland_extensions);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001166 Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
Chandler Carruth50465d12011-04-23 06:30:43 +00001167 Opts.ConstStrings = Args.hasFlag(OPT_fconst_strings, OPT_fno_const_strings,
1168 Opts.ConstStrings);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001169 if (Args.hasArg(OPT_fno_lax_vector_conversions))
Anders Carlssona508b7d2010-02-06 23:23:06 +00001170 Opts.LaxVectorConversions = 0;
1171 if (Args.hasArg(OPT_fno_threadsafe_statics))
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001172 Opts.ThreadsafeStatics = 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001173 Opts.Exceptions = Args.hasArg(OPT_fexceptions);
Anders Carlssone9b801f2011-02-22 01:52:06 +00001174 Opts.ObjCExceptions = Args.hasArg(OPT_fobjc_exceptions);
Anders Carlsson0761e442011-02-23 03:16:42 +00001175 Opts.CXXExceptions = Args.hasArg(OPT_fcxx_exceptions);
1176 Opts.SjLjExceptions = Args.hasArg(OPT_fsjlj_exceptions);
Daniel Dunbar2ed42282011-03-18 21:23:38 +00001177 Opts.TraditionalCPP = Args.hasArg(OPT_traditional_cpp);
Anders Carlsson0761e442011-02-23 03:16:42 +00001178
Mike Stumpde050572009-12-02 18:57:08 +00001179 Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001180 Opts.Blocks = Args.hasArg(OPT_fblocks);
John McCall13db5cf2011-09-09 20:41:01 +00001181 Opts.BlocksRuntimeOptional = Args.hasArg(OPT_fblocks_runtime_optional);
Douglas Gregor752c74d2012-01-03 17:07:34 +00001182 Opts.Modules = Args.hasArg(OPT_fmodules);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001183 Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +00001184 Opts.WChar = Opts.CPlusPlus && !Args.hasArg(OPT_fno_wchar);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001185 Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +00001186 Opts.ShortEnums = Args.hasArg(OPT_fshort_enums);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001187 Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
1188 Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
Nuno Lopesfc284482009-12-16 16:59:22 +00001189 Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001190 Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
John McCall7002f4c2010-04-09 19:03:51 +00001191 Opts.AccessControl = !Args.hasArg(OPT_fno_access_control);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001192 Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
Dan Gohmanc31176d2010-01-08 02:20:44 +00001193 Opts.MathErrno = Args.hasArg(OPT_fmath_errno);
Douglas Gregor8f4783c2012-07-02 19:56:23 +00001194 Opts.InstantiationDepth = Args.getLastArgIntValue(OPT_ftemplate_depth, 512,
Richard Smithc18c4232011-11-21 19:36:32 +00001195 Diags);
1196 Opts.ConstexprCallDepth = Args.getLastArgIntValue(OPT_fconstexpr_depth, 512,
1197 Diags);
Francois Pichet8387e2a2011-04-22 22:18:13 +00001198 Opts.DelayedTemplateParsing = Args.hasArg(OPT_fdelayed_template_parsing);
James Molloybfd7a522012-05-01 14:57:16 +00001199 Opts.NumLargeByValueCopy = Args.getLastArgIntValue(OPT_Wlarge_by_value_copy_EQ,
Argyrios Kyrtzidis3532fdd2010-11-17 23:11:54 +00001200 0, Diags);
Douglas Gregor6f755502011-02-01 15:15:22 +00001201 Opts.MSBitfields = Args.hasArg(OPT_mms_bitfields);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001202 Opts.ObjCConstantStringClass =
1203 Args.getLastArgValue(OPT_fconstant_string_class);
Ted Kremenekc32647d2010-12-23 21:35:43 +00001204 Opts.ObjCDefaultSynthProperties =
1205 Args.hasArg(OPT_fobjc_default_synthesize_properties);
Fariborz Jahanian3d145f62012-11-15 19:02:45 +00001206 Opts.EncodeExtendedBlockSig =
1207 Args.hasArg(OPT_fencode_extended_block_signature);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001208 Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
James Molloybfd7a522012-05-01 14:57:16 +00001209 Opts.PackStruct = Args.getLastArgIntValue(OPT_fpack_struct_EQ, 0, Diags);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001210 Opts.PICLevel = Args.getLastArgIntValue(OPT_pic_level, 0, Diags);
Chandler Carruth5e219cf2012-04-08 16:40:35 +00001211 Opts.PIELevel = Args.getLastArgIntValue(OPT_pie_level, 0, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001212 Opts.Static = Args.hasArg(OPT_static_define);
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001213 Opts.DumpRecordLayoutsSimple = Args.hasArg(OPT_fdump_record_layouts_simple);
1214 Opts.DumpRecordLayouts = Opts.DumpRecordLayoutsSimple
1215 || Args.hasArg(OPT_fdump_record_layouts);
Anders Carlsson046c2942010-04-17 20:15:18 +00001216 Opts.DumpVTableLayouts = Args.hasArg(OPT_fdump_vtable_layouts);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001217 Opts.SpellChecking = !Args.hasArg(OPT_fno_spell_checking);
Daniel Dunbarfb937b82010-04-15 15:06:22 +00001218 Opts.NoBitFieldTypeAlign = Args.hasArg(OPT_fno_bitfield_type_align);
Peter Collingbourne09821362010-12-04 01:50:56 +00001219 Opts.SinglePrecisionConstants = Args.hasArg(OPT_cl_single_precision_constant);
Peter Collingbournef0840822010-12-04 01:51:23 +00001220 Opts.FastRelaxedMath = Args.hasArg(OPT_cl_fast_relaxed_math);
Roman Divackycfe9af22011-03-01 17:40:53 +00001221 Opts.MRTD = Args.hasArg(OPT_mrtd);
Tony Linthicum96319392011-12-12 21:14:55 +00001222 Opts.HexagonQdsp6Compat = Args.hasArg(OPT_mqdsp6_compat);
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001223 Opts.FakeAddressSpaceMap = Args.hasArg(OPT_ffake_address_space_map);
John McCalla5fc4722011-04-09 22:50:59 +00001224 Opts.ParseUnknownAnytype = Args.hasArg(OPT_funknown_anytype);
John McCall48218c62011-07-13 17:56:40 +00001225 Opts.DebuggerSupport = Args.hasArg(OPT_fdebugger_support);
Sean Callanan50a9a122012-02-04 01:29:37 +00001226 Opts.DebuggerCastResultToId = Args.hasArg(OPT_fdebugger_cast_result_to_id);
Ted Kremenek30660a82012-03-06 20:06:33 +00001227 Opts.DebuggerObjCLiteral = Args.hasArg(OPT_fdebugger_objc_literal);
Eli Friedman19bda3a2011-11-02 01:53:16 +00001228 Opts.ApplePragmaPack = Args.hasArg(OPT_fapple_pragma_pack);
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001229 Opts.CurrentModule = Args.getLastArgValue(OPT_fmodule_name);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001230
Douglas Gregor50a70cd2013-01-15 06:45:29 +00001231 // Check if -fopenmp is specified.
1232 Opts.OpenMP = Args.hasArg(OPT_fopenmp);
1233
Chandler Carruth1cfe3c32011-04-23 09:27:53 +00001234 // Record whether the __DEPRECATED define was requested.
1235 Opts.Deprecated = Args.hasFlag(OPT_fdeprecated_macro,
1236 OPT_fno_deprecated_macro,
1237 Opts.Deprecated);
1238
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001239 // FIXME: Eliminate this dependency.
Dylan Noblesmithf23e8d22012-08-08 16:09:15 +00001240 unsigned Opt = getOptimizationLevel(Args, IK, Diags),
1241 OptSize = getOptimizationLevelSize(Args, IK, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001242 Opts.Optimize = Opt != 0;
Dylan Noblesmithf23e8d22012-08-08 16:09:15 +00001243 Opts.OptimizeSize = OptSize != 0;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001244
1245 // This is the __NO_INLINE__ define, which just depends on things like the
1246 // optimization level and -fno-inline, not actually whether the backend has
1247 // inlining enabled.
Chad Rosierfc055f92012-03-15 22:31:42 +00001248 Opts.NoInlineDefine = !Opt || Args.hasArg(OPT_fno_inline);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001249
Chandler Carruthabf07a72012-01-02 14:19:45 +00001250 Opts.FastMath = Args.hasArg(OPT_ffast_math);
Bob Wilson455e72e2012-07-19 03:52:53 +00001251 Opts.FiniteMathOnly = Args.hasArg(OPT_ffinite_math_only);
Chandler Carruthabf07a72012-01-02 14:19:45 +00001252
Ted Kremenek127ff2e2012-09-13 06:41:18 +00001253 Opts.RetainCommentsFromSystemHeaders =
1254 Args.hasArg(OPT_fretain_comments_from_system_headers);
1255
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001256 unsigned SSP = Args.getLastArgIntValue(OPT_stack_protector, 0, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001257 switch (SSP) {
1258 default:
1259 Diags.Report(diag::err_drv_invalid_value)
1260 << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
1261 break;
Douglas Gregore289d812011-09-13 17:21:33 +00001262 case 0: Opts.setStackProtector(LangOptions::SSPOff); break;
1263 case 1: Opts.setStackProtector(LangOptions::SSPOn); break;
1264 case 2: Opts.setStackProtector(LangOptions::SSPReq); break;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001265 }
Richard Smithc4dabad2012-11-05 22:04:41 +00001266
1267 // Parse -fsanitize= arguments.
1268 std::vector<std::string> Sanitizers = Args.getAllArgValues(OPT_fsanitize_EQ);
1269 for (unsigned I = 0, N = Sanitizers.size(); I != N; ++I) {
1270 // Since the Opts.Sanitize* values are bitfields, it's a little tricky to
1271 // efficiently map string values to them. Perform the mapping indirectly:
1272 // convert strings to enumerated values, then switch over the enum to set
1273 // the right bitfield value.
1274 enum Sanitizer {
1275#define SANITIZER(NAME, ID) \
1276 ID,
1277#include "clang/Basic/Sanitizers.def"
1278 Unknown
1279 };
1280 switch (llvm::StringSwitch<unsigned>(Sanitizers[I])
1281#define SANITIZER(NAME, ID) \
1282 .Case(NAME, ID)
1283#include "clang/Basic/Sanitizers.def"
1284 .Default(Unknown)) {
1285#define SANITIZER(NAME, ID) \
1286 case ID: \
Will Dietz4f45bc02013-01-18 11:30:38 +00001287 Opts.Sanitize.ID = true; \
Richard Smithc4dabad2012-11-05 22:04:41 +00001288 break;
1289#include "clang/Basic/Sanitizers.def"
1290
1291 case Unknown:
1292 Diags.Report(diag::err_drv_invalid_value)
1293 << "-fsanitize=" << Sanitizers[I];
1294 break;
1295 }
1296 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001297}
1298
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001299static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001300 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00001301 DiagnosticsEngine &Diags) {
James Molloybfd7a522012-05-01 14:57:16 +00001302 using namespace options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001303 Opts.ImplicitPCHInclude = Args.getLastArgValue(OPT_include_pch);
1304 Opts.ImplicitPTHInclude = Args.getLastArgValue(OPT_include_pth);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001305 if (const Arg *A = Args.getLastArg(OPT_token_cache))
Richard Smith1d489cf2012-11-01 04:30:05 +00001306 Opts.TokenCache = A->getValue();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001307 else
1308 Opts.TokenCache = Opts.ImplicitPTHInclude;
1309 Opts.UsePredefines = !Args.hasArg(OPT_undef);
Douglas Gregor94dc8f62010-03-19 16:15:56 +00001310 Opts.DetailedRecord = Args.hasArg(OPT_detailed_preprocessing_record);
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001311 Opts.DisablePCHValidation = Args.hasArg(OPT_fno_validate_pch);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +00001312
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +00001313 Opts.DumpDeserializedPCHDecls = Args.hasArg(OPT_dump_deserialized_pch_decls);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +00001314 for (arg_iterator it = Args.filtered_begin(OPT_error_on_deserialized_pch_decl),
1315 ie = Args.filtered_end(); it != ie; ++it) {
1316 const Arg *A = *it;
Richard Smith1d489cf2012-11-01 04:30:05 +00001317 Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue());
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +00001318 }
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001319
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +00001320 if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001321 StringRef Value(A->getValue());
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +00001322 size_t Comma = Value.find(',');
1323 unsigned Bytes = 0;
1324 unsigned EndOfLine = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001325
Chris Lattner5f9e2722011-07-23 10:55:15 +00001326 if (Comma == StringRef::npos ||
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +00001327 Value.substr(0, Comma).getAsInteger(10, Bytes) ||
1328 Value.substr(Comma + 1).getAsInteger(10, EndOfLine))
1329 Diags.Report(diag::err_drv_preamble_format);
1330 else {
1331 Opts.PrecompiledPreambleBytes.first = Bytes;
1332 Opts.PrecompiledPreambleBytes.second = (EndOfLine != 0);
1333 }
1334 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001335
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001336 // Add macros from the command line.
1337 for (arg_iterator it = Args.filtered_begin(OPT_D, OPT_U),
1338 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001339 if ((*it)->getOption().matches(OPT_D))
Richard Smith1d489cf2012-11-01 04:30:05 +00001340 Opts.addMacroDef((*it)->getValue());
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001341 else
Richard Smith1d489cf2012-11-01 04:30:05 +00001342 Opts.addMacroUndef((*it)->getValue());
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001343 }
1344
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001345 Opts.MacroIncludes = Args.getAllArgValues(OPT_imacros);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001346
1347 // Add the ordered list of -includes.
1348 for (arg_iterator it = Args.filtered_begin(OPT_include, OPT_include_pch,
1349 OPT_include_pth),
1350 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001351 const Arg *A = *it;
Richard Smith1d489cf2012-11-01 04:30:05 +00001352 Opts.Includes.push_back(A->getValue());
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001353 }
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001354
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00001355 for (arg_iterator it = Args.filtered_begin(OPT_chain_include),
1356 ie = Args.filtered_end(); it != ie; ++it) {
1357 const Arg *A = *it;
Richard Smith1d489cf2012-11-01 04:30:05 +00001358 Opts.ChainedIncludes.push_back(A->getValue());
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00001359 }
1360
Chris Lattnerdd173942010-04-14 03:54:58 +00001361 // Include 'altivec.h' if -faltivec option present
1362 if (Args.hasArg(OPT_faltivec))
1363 Opts.Includes.push_back("altivec.h");
1364
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001365 for (arg_iterator it = Args.filtered_begin(OPT_remap_file),
1366 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001367 const Arg *A = *it;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001368 std::pair<StringRef,StringRef> Split =
Richard Smith1d489cf2012-11-01 04:30:05 +00001369 StringRef(A->getValue()).split(';');
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001370
1371 if (Split.second.empty()) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001372 Diags.Report(diag::err_drv_invalid_remap_file) << A->getAsString(Args);
Daniel Dunbare14b5f52009-12-03 05:11:16 +00001373 continue;
1374 }
1375
1376 Opts.addRemappedFile(Split.first, Split.second);
1377 }
John McCallf85e1932011-06-15 23:02:42 +00001378
1379 if (Arg *A = Args.getLastArg(OPT_fobjc_arc_cxxlib_EQ)) {
Richard Smith1d489cf2012-11-01 04:30:05 +00001380 StringRef Name = A->getValue();
John McCallf85e1932011-06-15 23:02:42 +00001381 unsigned Library = llvm::StringSwitch<unsigned>(Name)
1382 .Case("libc++", ARCXX_libcxx)
1383 .Case("libstdc++", ARCXX_libstdcxx)
1384 .Case("none", ARCXX_nolib)
1385 .Default(~0U);
1386 if (Library == ~0U)
1387 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
1388 else
1389 Opts.ObjCXXARCStandardLibrary = (ObjCXXARCStandardLibraryKind)Library;
1390 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001391}
1392
1393static void ParsePreprocessorOutputArgs(PreprocessorOutputOptions &Opts,
1394 ArgList &Args) {
James Molloybfd7a522012-05-01 14:57:16 +00001395 using namespace options;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001396 Opts.ShowCPP = !Args.hasArg(OPT_dM);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001397 Opts.ShowComments = Args.hasArg(OPT_C);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001398 Opts.ShowLineMarkers = !Args.hasArg(OPT_P);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001399 Opts.ShowMacroComments = Args.hasArg(OPT_CC);
Daniel Dunbarf7c16d92010-08-24 22:44:13 +00001400 Opts.ShowMacros = Args.hasArg(OPT_dM) || Args.hasArg(OPT_dD);
David Blaikief1492f92012-06-14 17:36:09 +00001401 Opts.RewriteIncludes = Args.hasArg(OPT_frewrite_includes);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001402}
1403
1404static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) {
James Molloybfd7a522012-05-01 14:57:16 +00001405 using namespace options;
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001406 Opts.ABI = Args.getLastArgValue(OPT_target_abi);
Charles Davis98b7c5c2010-06-11 01:06:47 +00001407 Opts.CXXABI = Args.getLastArgValue(OPT_cxx_abi);
Daniel Dunbar03e8ab22010-05-20 16:54:55 +00001408 Opts.CPU = Args.getLastArgValue(OPT_target_cpu);
Douglas Gregor57016dd2012-10-16 23:40:58 +00001409 Opts.FeaturesAsWritten = Args.getAllArgValues(OPT_target_feature);
Daniel Dunbar39065a52010-08-11 23:07:42 +00001410 Opts.LinkerVersion = Args.getLastArgValue(OPT_target_linker_version);
Duncan Sands2dc14532010-08-30 09:42:39 +00001411 Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001412
Sebastian Pop5d8b9542011-11-01 21:33:06 +00001413 // Use the default target triple if unspecified.
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001414 if (Opts.Triple.empty())
Sebastian Pop5d8b9542011-11-01 21:33:06 +00001415 Opts.Triple = llvm::sys::getDefaultTargetTriple();
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001416}
1417
1418//
1419
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001420bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
Chris Lattner39b49bc2010-11-23 08:35:12 +00001421 const char *const *ArgBegin,
1422 const char *const *ArgEnd,
David Blaikied6471f72011-09-25 23:23:43 +00001423 DiagnosticsEngine &Diags) {
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001424 bool Success = true;
1425
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001426 // Parse the arguments.
James Molloybfd7a522012-05-01 14:57:16 +00001427 OwningPtr<OptTable> Opts(createDriverOptTable());
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001428 unsigned MissingArgIndex, MissingArgCount;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00001429 OwningPtr<InputArgList> Args(
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001430 Opts->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
1431
1432 // Check for missing argument error.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001433 if (MissingArgCount) {
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001434 Diags.Report(diag::err_drv_missing_argument)
1435 << Args->getArgString(MissingArgIndex) << MissingArgCount;
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001436 Success = false;
1437 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001438
1439 // Issue errors on unknown arguments.
1440 for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001441 ie = Args->filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001442 Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001443 Success = false;
1444 }
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001445
James Molloybfd7a522012-05-01 14:57:16 +00001446 // Issue errors on arguments that are not valid for CC1.
1447 for (ArgList::iterator I = Args->begin(), E = Args->end();
1448 I != E; ++I) {
Michael J. Spencer91e06da2012-10-19 22:37:06 +00001449 if (!(*I)->getOption().hasFlag(options::CC1Option)) {
James Molloybfd7a522012-05-01 14:57:16 +00001450 Diags.Report(diag::err_drv_unknown_argument) << (*I)->getAsString(*Args);
1451 Success = false;
1452 }
1453 }
1454
Ted Kremenek45796b12012-08-31 04:36:05 +00001455 Success = ParseAnalyzerArgs(*Res.getAnalyzerOpts(), *Args, Diags) && Success;
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +00001456 Success = ParseMigratorArgs(Res.getMigratorOpts(), *Args) && Success;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001457 ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), *Args);
Chad Rosier2dec85b2012-03-13 20:09:56 +00001458 Success = ParseDiagnosticArgs(Res.getDiagnosticOpts(), *Args, &Diags)
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001459 && Success;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001460 ParseFileSystemArgs(Res.getFileSystemOpts(), *Args);
Peter Collingbourne0a3c03c2010-12-04 01:50:36 +00001461 // FIXME: We shouldn't have to pass the DashX option around here
Daniel Dunbarc34ce3f2010-06-07 23:22:09 +00001462 InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001463 Success = ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, DashX, Diags)
1464 && Success;
Daniel Dunbar1e69fe32009-12-13 03:45:58 +00001465 ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args);
Fariborz Jahaniane293d302011-02-25 17:24:55 +00001466 if (DashX != IK_AST && DashX != IK_LLVM_IR) {
Ted Kremenekd3b74d92011-11-17 23:01:24 +00001467 ParseLangArgs(*Res.getLangOpts(), *Args, DashX, Diags);
Fariborz Jahaniane293d302011-02-25 17:24:55 +00001468 if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC)
Ted Kremenekd3b74d92011-11-17 23:01:24 +00001469 Res.getLangOpts()->ObjCExceptions = 1;
Fariborz Jahaniane293d302011-02-25 17:24:55 +00001470 }
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001471 // FIXME: ParsePreprocessorArgs uses the FileManager to read the contents of
1472 // PCH file and find the original header name. Remove the need to do that in
Chris Lattner39b49bc2010-11-23 08:35:12 +00001473 // ParsePreprocessorArgs and remove the FileManager
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001474 // parameters from the function and the "FileManager.h" #include.
Chris Lattner7ad97ff2010-11-23 07:51:02 +00001475 FileManager FileMgr(Res.getFileSystemOpts());
Chris Lattner39b49bc2010-11-23 08:35:12 +00001476 ParsePreprocessorArgs(Res.getPreprocessorOpts(), *Args, FileMgr, Diags);
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001477 ParsePreprocessorOutputArgs(Res.getPreprocessorOutputOpts(), *Args);
1478 ParseTargetArgs(Res.getTargetOpts(), *Args);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +00001479
1480 return Success;
Daniel Dunbar56ac85c2009-12-01 03:16:53 +00001481}
Douglas Gregor6e975c42011-09-13 23:15:45 +00001482
1483namespace {
1484
1485 class ModuleSignature {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001486 SmallVector<uint64_t, 16> Data;
Douglas Gregor6e975c42011-09-13 23:15:45 +00001487 unsigned CurBit;
1488 uint64_t CurValue;
1489
1490 public:
1491 ModuleSignature() : CurBit(0), CurValue(0) { }
1492
1493 void add(uint64_t Value, unsigned Bits);
1494 void add(StringRef Value);
1495 void flush();
1496
1497 llvm::APInt getAsInteger() const;
1498 };
1499}
1500
1501void ModuleSignature::add(uint64_t Value, unsigned int NumBits) {
1502 CurValue |= Value << CurBit;
1503 if (CurBit + NumBits < 64) {
1504 CurBit += NumBits;
1505 return;
1506 }
1507
1508 // Add the current word.
1509 Data.push_back(CurValue);
1510
1511 if (CurBit)
1512 CurValue = Value >> (64-CurBit);
1513 else
1514 CurValue = 0;
1515 CurBit = (CurBit+NumBits) & 63;
1516}
1517
1518void ModuleSignature::flush() {
1519 if (CurBit == 0)
1520 return;
1521
1522 Data.push_back(CurValue);
1523 CurBit = 0;
1524 CurValue = 0;
1525}
1526
1527void ModuleSignature::add(StringRef Value) {
1528 for (StringRef::iterator I = Value.begin(), IEnd = Value.end(); I != IEnd;++I)
1529 add(*I, 8);
1530}
1531
1532llvm::APInt ModuleSignature::getAsInteger() const {
1533 return llvm::APInt(Data.size() * 64, Data);
1534}
1535
1536std::string CompilerInvocation::getModuleHash() const {
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001537 using llvm::hash_code;
1538 using llvm::hash_value;
1539 using llvm::hash_combine;
1540
Douglas Gregor6e975c42011-09-13 23:15:45 +00001541 // Start the signature with the compiler version.
Douglas Gregor30431752012-11-05 23:30:26 +00001542 // FIXME: We'd rather use something more cryptographically sound than
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001543 // CityHash, but this will do for now.
1544 hash_code code = hash_value(getClangFullRepositoryVersion());
1545
Douglas Gregor6e975c42011-09-13 23:15:45 +00001546 // Extend the signature with the language options
1547#define LANGOPT(Name, Bits, Default, Description) \
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001548 code = hash_combine(code, LangOpts->Name);
Douglas Gregor6e975c42011-09-13 23:15:45 +00001549#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001550 code = hash_combine(code, static_cast<unsigned>(LangOpts->get##Name()));
Douglas Gregor6e975c42011-09-13 23:15:45 +00001551#define BENIGN_LANGOPT(Name, Bits, Default, Description)
1552#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
1553#include "clang/Basic/LangOptions.def"
1554
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001555 // Extend the signature with the target options.
1556 code = hash_combine(code, TargetOpts->Triple, TargetOpts->CPU,
1557 TargetOpts->ABI, TargetOpts->CXXABI,
1558 TargetOpts->LinkerVersion);
1559 for (unsigned i = 0, n = TargetOpts->FeaturesAsWritten.size(); i != n; ++i)
1560 code = hash_combine(code, TargetOpts->FeaturesAsWritten[i]);
Douglas Gregor6e975c42011-09-13 23:15:45 +00001561
Douglas Gregor815759c2011-09-14 15:55:12 +00001562 // Extend the signature with preprocessor options.
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001563 const PreprocessorOptions &ppOpts = getPreprocessorOpts();
1564 code = hash_combine(code, ppOpts.UsePredefines, ppOpts.DetailedRecord);
1565
Douglas Gregor05edf662011-10-17 14:55:37 +00001566 std::vector<StringRef> MacroDefs;
1567 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
1568 I = getPreprocessorOpts().Macros.begin(),
1569 IEnd = getPreprocessorOpts().Macros.end();
1570 I != IEnd; ++I) {
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001571 code = hash_combine(code, I->first, I->second);
Douglas Gregor05edf662011-10-17 14:55:37 +00001572 }
Douglas Gregor984e2eb2012-11-05 19:45:09 +00001573
1574 // Extend the signature with the sysroot.
1575 const HeaderSearchOptions &hsOpts = getHeaderSearchOpts();
1576 code = hash_combine(code, hsOpts.Sysroot, hsOpts.UseBuiltinIncludes,
1577 hsOpts.UseStandardSystemIncludes,
1578 hsOpts.UseStandardCXXIncludes,
1579 hsOpts.UseLibcxx);
1580
1581 return llvm::APInt(64, code).toString(36, /*Signed=*/false);
Douglas Gregor6e975c42011-09-13 23:15:45 +00001582}