xref: /minix3/external/bsd/llvm/dist/clang/lib/Frontend/CompilerInvocation.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- CompilerInvocation.cpp -------------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInvocation.h"
11f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
12f4a2713aSLionel Sambuc #include "clang/Basic/Version.h"
13*0a6a1f1dSLionel Sambuc #include "clang/Config/config.h"
14f4a2713aSLionel Sambuc #include "clang/Driver/DriverDiagnostic.h"
15f4a2713aSLionel Sambuc #include "clang/Driver/Options.h"
16f4a2713aSLionel Sambuc #include "clang/Driver/Util.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Frontend/FrontendDiagnostic.h"
18f4a2713aSLionel Sambuc #include "clang/Frontend/LangStandard.h"
19f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
20f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearchOptions.h"
21f4a2713aSLionel Sambuc #include "clang/Serialization/ASTReader.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/Hashing.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/ADT/STLExtras.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
27f4a2713aSLionel Sambuc #include "llvm/ADT/Triple.h"
28f4a2713aSLionel Sambuc #include "llvm/Option/Arg.h"
29f4a2713aSLionel Sambuc #include "llvm/Option/ArgList.h"
30f4a2713aSLionel Sambuc #include "llvm/Option/OptTable.h"
31f4a2713aSLionel Sambuc #include "llvm/Option/Option.h"
32*0a6a1f1dSLionel Sambuc #include "llvm/Support/CodeGen.h"
33f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
34f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
35f4a2713aSLionel Sambuc #include "llvm/Support/Host.h"
36f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
37f4a2713aSLionel Sambuc #include "llvm/Support/Process.h"
38*0a6a1f1dSLionel Sambuc #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix)
39*0a6a1f1dSLionel Sambuc #include <atomic>
40*0a6a1f1dSLionel Sambuc #endif // !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix)
41*0a6a1f1dSLionel Sambuc #include <memory>
42f4a2713aSLionel Sambuc #include <sys/stat.h>
43*0a6a1f1dSLionel Sambuc #include <system_error>
44f4a2713aSLionel Sambuc using namespace clang;
45f4a2713aSLionel Sambuc 
46f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
47f4a2713aSLionel Sambuc // Initialization.
48f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
49f4a2713aSLionel Sambuc 
CompilerInvocationBase()50f4a2713aSLionel Sambuc CompilerInvocationBase::CompilerInvocationBase()
51f4a2713aSLionel Sambuc   : LangOpts(new LangOptions()), TargetOpts(new TargetOptions()),
52f4a2713aSLionel Sambuc     DiagnosticOpts(new DiagnosticOptions()),
53f4a2713aSLionel Sambuc     HeaderSearchOpts(new HeaderSearchOptions()),
54f4a2713aSLionel Sambuc     PreprocessorOpts(new PreprocessorOptions()) {}
55f4a2713aSLionel Sambuc 
CompilerInvocationBase(const CompilerInvocationBase & X)56f4a2713aSLionel Sambuc CompilerInvocationBase::CompilerInvocationBase(const CompilerInvocationBase &X)
57f4a2713aSLionel Sambuc   : RefCountedBase<CompilerInvocation>(),
58f4a2713aSLionel Sambuc     LangOpts(new LangOptions(*X.getLangOpts())),
59f4a2713aSLionel Sambuc     TargetOpts(new TargetOptions(X.getTargetOpts())),
60f4a2713aSLionel Sambuc     DiagnosticOpts(new DiagnosticOptions(X.getDiagnosticOpts())),
61f4a2713aSLionel Sambuc     HeaderSearchOpts(new HeaderSearchOptions(X.getHeaderSearchOpts())),
62f4a2713aSLionel Sambuc     PreprocessorOpts(new PreprocessorOptions(X.getPreprocessorOpts())) {}
63f4a2713aSLionel Sambuc 
~CompilerInvocationBase()64*0a6a1f1dSLionel Sambuc CompilerInvocationBase::~CompilerInvocationBase() {}
65*0a6a1f1dSLionel Sambuc 
66f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
67f4a2713aSLionel Sambuc // Deserialization (from args)
68f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc using namespace clang::driver;
71f4a2713aSLionel Sambuc using namespace clang::driver::options;
72f4a2713aSLionel Sambuc using namespace llvm::opt;
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc //
75f4a2713aSLionel Sambuc 
getOptimizationLevel(ArgList & Args,InputKind IK,DiagnosticsEngine & Diags)76f4a2713aSLionel Sambuc static unsigned getOptimizationLevel(ArgList &Args, InputKind IK,
77f4a2713aSLionel Sambuc                                      DiagnosticsEngine &Diags) {
78f4a2713aSLionel Sambuc   unsigned DefaultOpt = 0;
79f4a2713aSLionel Sambuc   if (IK == IK_OpenCL && !Args.hasArg(OPT_cl_opt_disable))
80f4a2713aSLionel Sambuc     DefaultOpt = 2;
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
83f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_O0))
84f4a2713aSLionel Sambuc       return 0;
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_Ofast))
87f4a2713aSLionel Sambuc       return 3;
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc     assert (A->getOption().matches(options::OPT_O));
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc     StringRef S(A->getValue());
92f4a2713aSLionel Sambuc     if (S == "s" || S == "z" || S.empty())
93f4a2713aSLionel Sambuc       return 2;
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc     return getLastArgIntValue(Args, OPT_O, DefaultOpt, Diags);
96f4a2713aSLionel Sambuc   }
97f4a2713aSLionel Sambuc 
98f4a2713aSLionel Sambuc   return DefaultOpt;
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
getOptimizationLevelSize(ArgList & Args)101f4a2713aSLionel Sambuc static unsigned getOptimizationLevelSize(ArgList &Args) {
102f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
103f4a2713aSLionel Sambuc     if (A->getOption().matches(options::OPT_O)) {
104f4a2713aSLionel Sambuc       switch (A->getValue()[0]) {
105f4a2713aSLionel Sambuc       default:
106f4a2713aSLionel Sambuc         return 0;
107f4a2713aSLionel Sambuc       case 's':
108f4a2713aSLionel Sambuc         return 1;
109f4a2713aSLionel Sambuc       case 'z':
110f4a2713aSLionel Sambuc         return 2;
111f4a2713aSLionel Sambuc       }
112f4a2713aSLionel Sambuc     }
113f4a2713aSLionel Sambuc   }
114f4a2713aSLionel Sambuc   return 0;
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc 
addDiagnosticArgs(ArgList & Args,OptSpecifier Group,OptSpecifier GroupWithValue,std::vector<std::string> & Diagnostics)117*0a6a1f1dSLionel Sambuc static void addDiagnosticArgs(ArgList &Args, OptSpecifier Group,
118*0a6a1f1dSLionel Sambuc                               OptSpecifier GroupWithValue,
119*0a6a1f1dSLionel Sambuc                               std::vector<std::string> &Diagnostics) {
120*0a6a1f1dSLionel Sambuc   for (Arg *A : Args.filtered(Group)) {
121f4a2713aSLionel Sambuc     if (A->getOption().getKind() == Option::FlagClass) {
122*0a6a1f1dSLionel Sambuc       // The argument is a pure flag (such as OPT_Wall or OPT_Wdeprecated). Add
123*0a6a1f1dSLionel Sambuc       // its name (minus the "W" or "R" at the beginning) to the warning list.
124*0a6a1f1dSLionel Sambuc       Diagnostics.push_back(A->getOption().getName().drop_front(1));
125*0a6a1f1dSLionel Sambuc     } else if (A->getOption().matches(GroupWithValue)) {
126*0a6a1f1dSLionel Sambuc       // This is -Wfoo= or -Rfoo=, where foo is the name of the diagnostic group.
127*0a6a1f1dSLionel Sambuc       Diagnostics.push_back(A->getOption().getName().drop_front(1).rtrim("=-"));
128f4a2713aSLionel Sambuc     } else {
129*0a6a1f1dSLionel Sambuc       // Otherwise, add its value (for OPT_W_Joined and similar).
130*0a6a1f1dSLionel Sambuc       for (const char *Arg : A->getValues())
131*0a6a1f1dSLionel Sambuc         Diagnostics.push_back(Arg);
132f4a2713aSLionel Sambuc     }
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
ParseAnalyzerArgs(AnalyzerOptions & Opts,ArgList & Args,DiagnosticsEngine & Diags)136f4a2713aSLionel Sambuc static bool ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args,
137f4a2713aSLionel Sambuc                               DiagnosticsEngine &Diags) {
138f4a2713aSLionel Sambuc   using namespace options;
139f4a2713aSLionel Sambuc   bool Success = true;
140f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_analyzer_store)) {
141f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
142f4a2713aSLionel Sambuc     AnalysisStores Value = llvm::StringSwitch<AnalysisStores>(Name)
143f4a2713aSLionel Sambuc #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) \
144f4a2713aSLionel Sambuc       .Case(CMDFLAG, NAME##Model)
145f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Analyses.def"
146f4a2713aSLionel Sambuc       .Default(NumStores);
147f4a2713aSLionel Sambuc     if (Value == NumStores) {
148f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
149f4a2713aSLionel Sambuc         << A->getAsString(Args) << Name;
150f4a2713aSLionel Sambuc       Success = false;
151f4a2713aSLionel Sambuc     } else {
152f4a2713aSLionel Sambuc       Opts.AnalysisStoreOpt = Value;
153f4a2713aSLionel Sambuc     }
154f4a2713aSLionel Sambuc   }
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_analyzer_constraints)) {
157f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
158f4a2713aSLionel Sambuc     AnalysisConstraints Value = llvm::StringSwitch<AnalysisConstraints>(Name)
159f4a2713aSLionel Sambuc #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) \
160f4a2713aSLionel Sambuc       .Case(CMDFLAG, NAME##Model)
161f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Analyses.def"
162f4a2713aSLionel Sambuc       .Default(NumConstraints);
163f4a2713aSLionel Sambuc     if (Value == NumConstraints) {
164f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
165f4a2713aSLionel Sambuc         << A->getAsString(Args) << Name;
166f4a2713aSLionel Sambuc       Success = false;
167f4a2713aSLionel Sambuc     } else {
168f4a2713aSLionel Sambuc       Opts.AnalysisConstraintsOpt = Value;
169f4a2713aSLionel Sambuc     }
170f4a2713aSLionel Sambuc   }
171f4a2713aSLionel Sambuc 
172f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_analyzer_output)) {
173f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
174f4a2713aSLionel Sambuc     AnalysisDiagClients Value = llvm::StringSwitch<AnalysisDiagClients>(Name)
175f4a2713aSLionel Sambuc #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN) \
176f4a2713aSLionel Sambuc       .Case(CMDFLAG, PD_##NAME)
177f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Analyses.def"
178f4a2713aSLionel Sambuc       .Default(NUM_ANALYSIS_DIAG_CLIENTS);
179f4a2713aSLionel Sambuc     if (Value == NUM_ANALYSIS_DIAG_CLIENTS) {
180f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
181f4a2713aSLionel Sambuc         << A->getAsString(Args) << Name;
182f4a2713aSLionel Sambuc       Success = false;
183f4a2713aSLionel Sambuc     } else {
184f4a2713aSLionel Sambuc       Opts.AnalysisDiagOpt = Value;
185f4a2713aSLionel Sambuc     }
186f4a2713aSLionel Sambuc   }
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_analyzer_purge)) {
189f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
190f4a2713aSLionel Sambuc     AnalysisPurgeMode Value = llvm::StringSwitch<AnalysisPurgeMode>(Name)
191f4a2713aSLionel Sambuc #define ANALYSIS_PURGE(NAME, CMDFLAG, DESC) \
192f4a2713aSLionel Sambuc       .Case(CMDFLAG, NAME)
193f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Analyses.def"
194f4a2713aSLionel Sambuc       .Default(NumPurgeModes);
195f4a2713aSLionel Sambuc     if (Value == NumPurgeModes) {
196f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
197f4a2713aSLionel Sambuc         << A->getAsString(Args) << Name;
198f4a2713aSLionel Sambuc       Success = false;
199f4a2713aSLionel Sambuc     } else {
200f4a2713aSLionel Sambuc       Opts.AnalysisPurgeOpt = Value;
201f4a2713aSLionel Sambuc     }
202f4a2713aSLionel Sambuc   }
203f4a2713aSLionel Sambuc 
204f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_analyzer_inlining_mode)) {
205f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
206f4a2713aSLionel Sambuc     AnalysisInliningMode Value = llvm::StringSwitch<AnalysisInliningMode>(Name)
207f4a2713aSLionel Sambuc #define ANALYSIS_INLINING_MODE(NAME, CMDFLAG, DESC) \
208f4a2713aSLionel Sambuc       .Case(CMDFLAG, NAME)
209f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Analyses.def"
210f4a2713aSLionel Sambuc       .Default(NumInliningModes);
211f4a2713aSLionel Sambuc     if (Value == NumInliningModes) {
212f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
213f4a2713aSLionel Sambuc         << A->getAsString(Args) << Name;
214f4a2713aSLionel Sambuc       Success = false;
215f4a2713aSLionel Sambuc     } else {
216f4a2713aSLionel Sambuc       Opts.InliningMode = Value;
217f4a2713aSLionel Sambuc     }
218f4a2713aSLionel Sambuc   }
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc   Opts.ShowCheckerHelp = Args.hasArg(OPT_analyzer_checker_help);
221*0a6a1f1dSLionel Sambuc   Opts.DisableAllChecks = Args.hasArg(OPT_analyzer_disable_all_checks);
222*0a6a1f1dSLionel Sambuc 
223f4a2713aSLionel Sambuc   Opts.visualizeExplodedGraphWithGraphViz =
224f4a2713aSLionel Sambuc     Args.hasArg(OPT_analyzer_viz_egraph_graphviz);
225f4a2713aSLionel Sambuc   Opts.visualizeExplodedGraphWithUbiGraph =
226f4a2713aSLionel Sambuc     Args.hasArg(OPT_analyzer_viz_egraph_ubigraph);
227f4a2713aSLionel Sambuc   Opts.NoRetryExhausted = Args.hasArg(OPT_analyzer_disable_retry_exhausted);
228f4a2713aSLionel Sambuc   Opts.AnalyzeAll = Args.hasArg(OPT_analyzer_opt_analyze_headers);
229f4a2713aSLionel Sambuc   Opts.AnalyzerDisplayProgress = Args.hasArg(OPT_analyzer_display_progress);
230f4a2713aSLionel Sambuc   Opts.AnalyzeNestedBlocks =
231f4a2713aSLionel Sambuc     Args.hasArg(OPT_analyzer_opt_analyze_nested_blocks);
232f4a2713aSLionel Sambuc   Opts.eagerlyAssumeBinOpBifurcation = Args.hasArg(OPT_analyzer_eagerly_assume);
233f4a2713aSLionel Sambuc   Opts.AnalyzeSpecificFunction = Args.getLastArgValue(OPT_analyze_function);
234f4a2713aSLionel Sambuc   Opts.UnoptimizedCFG = Args.hasArg(OPT_analysis_UnoptimizedCFG);
235f4a2713aSLionel Sambuc   Opts.TrimGraph = Args.hasArg(OPT_trim_egraph);
236f4a2713aSLionel Sambuc   Opts.maxBlockVisitOnPath =
237f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_analyzer_max_loop, 4, Diags);
238f4a2713aSLionel Sambuc   Opts.PrintStats = Args.hasArg(OPT_analyzer_stats);
239f4a2713aSLionel Sambuc   Opts.InlineMaxStackDepth =
240f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_analyzer_inline_max_stack_depth,
241f4a2713aSLionel Sambuc                          Opts.InlineMaxStackDepth, Diags);
242f4a2713aSLionel Sambuc 
243f4a2713aSLionel Sambuc   Opts.CheckersControlList.clear();
244f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_analyzer_checker,
245f4a2713aSLionel Sambuc                                              OPT_analyzer_disable_checker),
246f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
247f4a2713aSLionel Sambuc     const Arg *A = *it;
248f4a2713aSLionel Sambuc     A->claim();
249f4a2713aSLionel Sambuc     bool enable = (A->getOption().getID() == OPT_analyzer_checker);
250f4a2713aSLionel Sambuc     // We can have a list of comma separated checker names, e.g:
251f4a2713aSLionel Sambuc     // '-analyzer-checker=cocoa,unix'
252f4a2713aSLionel Sambuc     StringRef checkerList = A->getValue();
253f4a2713aSLionel Sambuc     SmallVector<StringRef, 4> checkers;
254f4a2713aSLionel Sambuc     checkerList.split(checkers, ",");
255f4a2713aSLionel Sambuc     for (unsigned i = 0, e = checkers.size(); i != e; ++i)
256f4a2713aSLionel Sambuc       Opts.CheckersControlList.push_back(std::make_pair(checkers[i], enable));
257f4a2713aSLionel Sambuc   }
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc   // Go through the analyzer configuration options.
260f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_analyzer_config),
261f4a2713aSLionel Sambuc        ie = Args.filtered_end(); it != ie; ++it) {
262f4a2713aSLionel Sambuc     const Arg *A = *it;
263f4a2713aSLionel Sambuc     A->claim();
264f4a2713aSLionel Sambuc     // We can have a list of comma separated config names, e.g:
265f4a2713aSLionel Sambuc     // '-analyzer-config key1=val1,key2=val2'
266f4a2713aSLionel Sambuc     StringRef configList = A->getValue();
267f4a2713aSLionel Sambuc     SmallVector<StringRef, 4> configVals;
268f4a2713aSLionel Sambuc     configList.split(configVals, ",");
269f4a2713aSLionel Sambuc     for (unsigned i = 0, e = configVals.size(); i != e; ++i) {
270f4a2713aSLionel Sambuc       StringRef key, val;
271*0a6a1f1dSLionel Sambuc       std::tie(key, val) = configVals[i].split("=");
272f4a2713aSLionel Sambuc       if (val.empty()) {
273f4a2713aSLionel Sambuc         Diags.Report(SourceLocation(),
274f4a2713aSLionel Sambuc                      diag::err_analyzer_config_no_value) << configVals[i];
275f4a2713aSLionel Sambuc         Success = false;
276f4a2713aSLionel Sambuc         break;
277f4a2713aSLionel Sambuc       }
278f4a2713aSLionel Sambuc       if (val.find('=') != StringRef::npos) {
279f4a2713aSLionel Sambuc         Diags.Report(SourceLocation(),
280f4a2713aSLionel Sambuc                      diag::err_analyzer_config_multiple_values)
281f4a2713aSLionel Sambuc           << configVals[i];
282f4a2713aSLionel Sambuc         Success = false;
283f4a2713aSLionel Sambuc         break;
284f4a2713aSLionel Sambuc       }
285f4a2713aSLionel Sambuc       Opts.Config[key] = val;
286f4a2713aSLionel Sambuc     }
287f4a2713aSLionel Sambuc   }
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc   return Success;
290f4a2713aSLionel Sambuc }
291f4a2713aSLionel Sambuc 
ParseMigratorArgs(MigratorOptions & Opts,ArgList & Args)292f4a2713aSLionel Sambuc static bool ParseMigratorArgs(MigratorOptions &Opts, ArgList &Args) {
293f4a2713aSLionel Sambuc   Opts.NoNSAllocReallocError = Args.hasArg(OPT_migrator_no_nsalloc_error);
294f4a2713aSLionel Sambuc   Opts.NoFinalizeRemoval = Args.hasArg(OPT_migrator_no_finalize_removal);
295f4a2713aSLionel Sambuc   return true;
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc 
ParseCommentArgs(CommentOptions & Opts,ArgList & Args)298f4a2713aSLionel Sambuc static void ParseCommentArgs(CommentOptions &Opts, ArgList &Args) {
299f4a2713aSLionel Sambuc   Opts.BlockCommandNames = Args.getAllArgValues(OPT_fcomment_block_commands);
300f4a2713aSLionel Sambuc   Opts.ParseAllComments = Args.hasArg(OPT_fparse_all_comments);
301f4a2713aSLionel Sambuc }
302f4a2713aSLionel Sambuc 
getCodeModel(ArgList & Args,DiagnosticsEngine & Diags)303*0a6a1f1dSLionel Sambuc static StringRef getCodeModel(ArgList &Args, DiagnosticsEngine &Diags) {
304*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_mcode_model)) {
305*0a6a1f1dSLionel Sambuc     StringRef Value = A->getValue();
306*0a6a1f1dSLionel Sambuc     if (Value == "small" || Value == "kernel" || Value == "medium" ||
307*0a6a1f1dSLionel Sambuc         Value == "large")
308*0a6a1f1dSLionel Sambuc       return Value;
309*0a6a1f1dSLionel Sambuc     Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Value;
310*0a6a1f1dSLionel Sambuc   }
311*0a6a1f1dSLionel Sambuc   return "default";
312*0a6a1f1dSLionel Sambuc }
313*0a6a1f1dSLionel Sambuc 
314*0a6a1f1dSLionel Sambuc /// \brief Create a new Regex instance out of the string value in \p RpassArg.
315*0a6a1f1dSLionel Sambuc /// It returns a pointer to the newly generated Regex instance.
316*0a6a1f1dSLionel Sambuc static std::shared_ptr<llvm::Regex>
GenerateOptimizationRemarkRegex(DiagnosticsEngine & Diags,ArgList & Args,Arg * RpassArg)317*0a6a1f1dSLionel Sambuc GenerateOptimizationRemarkRegex(DiagnosticsEngine &Diags, ArgList &Args,
318*0a6a1f1dSLionel Sambuc                                 Arg *RpassArg) {
319*0a6a1f1dSLionel Sambuc   StringRef Val = RpassArg->getValue();
320*0a6a1f1dSLionel Sambuc   std::string RegexError;
321*0a6a1f1dSLionel Sambuc   std::shared_ptr<llvm::Regex> Pattern = std::make_shared<llvm::Regex>(Val);
322*0a6a1f1dSLionel Sambuc   if (!Pattern->isValid(RegexError)) {
323*0a6a1f1dSLionel Sambuc     Diags.Report(diag::err_drv_optimization_remark_pattern)
324*0a6a1f1dSLionel Sambuc         << RegexError << RpassArg->getAsString(Args);
325*0a6a1f1dSLionel Sambuc     Pattern.reset();
326*0a6a1f1dSLionel Sambuc   }
327*0a6a1f1dSLionel Sambuc   return Pattern;
328*0a6a1f1dSLionel Sambuc }
329*0a6a1f1dSLionel Sambuc 
parseSanitizerKinds(StringRef FlagName,const std::vector<std::string> & Sanitizers,DiagnosticsEngine & Diags,SanitizerSet & S)330*0a6a1f1dSLionel Sambuc static void parseSanitizerKinds(StringRef FlagName,
331*0a6a1f1dSLionel Sambuc                                 const std::vector<std::string> &Sanitizers,
332*0a6a1f1dSLionel Sambuc                                 DiagnosticsEngine &Diags, SanitizerSet &S) {
333*0a6a1f1dSLionel Sambuc   for (const auto &Sanitizer : Sanitizers) {
334*0a6a1f1dSLionel Sambuc     SanitizerKind K = llvm::StringSwitch<SanitizerKind>(Sanitizer)
335*0a6a1f1dSLionel Sambuc #define SANITIZER(NAME, ID) .Case(NAME, SanitizerKind::ID)
336*0a6a1f1dSLionel Sambuc #include "clang/Basic/Sanitizers.def"
337*0a6a1f1dSLionel Sambuc                           .Default(SanitizerKind::Unknown);
338*0a6a1f1dSLionel Sambuc     if (K == SanitizerKind::Unknown)
339*0a6a1f1dSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value) << FlagName << Sanitizer;
340*0a6a1f1dSLionel Sambuc     else
341*0a6a1f1dSLionel Sambuc       S.set(K, true);
342*0a6a1f1dSLionel Sambuc   }
343*0a6a1f1dSLionel Sambuc }
344*0a6a1f1dSLionel Sambuc 
ParseCodeGenArgs(CodeGenOptions & Opts,ArgList & Args,InputKind IK,DiagnosticsEngine & Diags,const TargetOptions & TargetOpts)345f4a2713aSLionel Sambuc static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
346*0a6a1f1dSLionel Sambuc                              DiagnosticsEngine &Diags,
347*0a6a1f1dSLionel Sambuc                              const TargetOptions &TargetOpts) {
348f4a2713aSLionel Sambuc   using namespace options;
349f4a2713aSLionel Sambuc   bool Success = true;
350f4a2713aSLionel Sambuc 
351*0a6a1f1dSLionel Sambuc   unsigned OptimizationLevel = getOptimizationLevel(Args, IK, Diags);
352f4a2713aSLionel Sambuc   // TODO: This could be done in Driver
353f4a2713aSLionel Sambuc   unsigned MaxOptLevel = 3;
354*0a6a1f1dSLionel Sambuc   if (OptimizationLevel > MaxOptLevel) {
355*0a6a1f1dSLionel Sambuc     // If the optimization level is not supported, fall back on the default
356*0a6a1f1dSLionel Sambuc     // optimization
357f4a2713aSLionel Sambuc     Diags.Report(diag::warn_drv_optimization_value)
358f4a2713aSLionel Sambuc         << Args.getLastArg(OPT_O)->getAsString(Args) << "-O" << MaxOptLevel;
359*0a6a1f1dSLionel Sambuc     OptimizationLevel = MaxOptLevel;
360f4a2713aSLionel Sambuc   }
361*0a6a1f1dSLionel Sambuc   Opts.OptimizationLevel = OptimizationLevel;
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   // We must always run at least the always inlining pass.
364f4a2713aSLionel Sambuc   Opts.setInlining(
365f4a2713aSLionel Sambuc     (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
366f4a2713aSLionel Sambuc                                  : CodeGenOptions::OnlyAlwaysInlining);
367f4a2713aSLionel Sambuc   // -fno-inline-functions overrides OptimizationLevel > 1.
368f4a2713aSLionel Sambuc   Opts.NoInline = Args.hasArg(OPT_fno_inline);
369f4a2713aSLionel Sambuc   Opts.setInlining(Args.hasArg(OPT_fno_inline_functions) ?
370f4a2713aSLionel Sambuc                      CodeGenOptions::OnlyAlwaysInlining : Opts.getInlining());
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_gline_tables_only)) {
373f4a2713aSLionel Sambuc     Opts.setDebugInfo(CodeGenOptions::DebugLineTablesOnly);
374f4a2713aSLionel Sambuc   } else if (Args.hasArg(OPT_g_Flag) || Args.hasArg(OPT_gdwarf_2) ||
375f4a2713aSLionel Sambuc              Args.hasArg(OPT_gdwarf_3) || Args.hasArg(OPT_gdwarf_4)) {
376*0a6a1f1dSLionel Sambuc     bool Default = false;
377*0a6a1f1dSLionel Sambuc     // Until dtrace (via CTF) and LLDB can deal with distributed debug info,
378*0a6a1f1dSLionel Sambuc     // Darwin and FreeBSD default to standalone/full debug info.
379*0a6a1f1dSLionel Sambuc     if (llvm::Triple(TargetOpts.Triple).isOSDarwin() ||
380*0a6a1f1dSLionel Sambuc         llvm::Triple(TargetOpts.Triple).isOSFreeBSD())
381*0a6a1f1dSLionel Sambuc       Default = true;
382*0a6a1f1dSLionel Sambuc 
383*0a6a1f1dSLionel Sambuc     if (Args.hasFlag(OPT_fstandalone_debug, OPT_fno_standalone_debug, Default))
384f4a2713aSLionel Sambuc       Opts.setDebugInfo(CodeGenOptions::FullDebugInfo);
385*0a6a1f1dSLionel Sambuc     else
386*0a6a1f1dSLionel Sambuc       Opts.setDebugInfo(CodeGenOptions::LimitedDebugInfo);
387f4a2713aSLionel Sambuc   }
388f4a2713aSLionel Sambuc   Opts.DebugColumnInfo = Args.hasArg(OPT_dwarf_column_info);
389f4a2713aSLionel Sambuc   Opts.SplitDwarfFile = Args.getLastArgValue(OPT_split_dwarf_file);
390f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_gdwarf_2))
391f4a2713aSLionel Sambuc     Opts.DwarfVersion = 2;
392f4a2713aSLionel Sambuc   else if (Args.hasArg(OPT_gdwarf_3))
393f4a2713aSLionel Sambuc     Opts.DwarfVersion = 3;
394f4a2713aSLionel Sambuc   else if (Args.hasArg(OPT_gdwarf_4))
395f4a2713aSLionel Sambuc     Opts.DwarfVersion = 4;
396f4a2713aSLionel Sambuc   else if (Opts.getDebugInfo() != CodeGenOptions::NoDebugInfo)
397f4a2713aSLionel Sambuc     // Default Dwarf version is 4 if we are generating debug information.
398f4a2713aSLionel Sambuc     Opts.DwarfVersion = 4;
399f4a2713aSLionel Sambuc 
400f4a2713aSLionel Sambuc   Opts.DisableLLVMOpts = Args.hasArg(OPT_disable_llvm_optzns);
401f4a2713aSLionel Sambuc   Opts.DisableRedZone = Args.hasArg(OPT_disable_red_zone);
402f4a2713aSLionel Sambuc   Opts.ForbidGuardVariables = Args.hasArg(OPT_fforbid_guard_variables);
403f4a2713aSLionel Sambuc   Opts.UseRegisterSizedBitfieldAccess = Args.hasArg(
404f4a2713aSLionel Sambuc     OPT_fuse_register_sized_bitfield_access);
405f4a2713aSLionel Sambuc   Opts.RelaxedAliasing = Args.hasArg(OPT_relaxed_aliasing);
406f4a2713aSLionel Sambuc   Opts.StructPathTBAA = !Args.hasArg(OPT_no_struct_path_tbaa);
407f4a2713aSLionel Sambuc   Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags);
408f4a2713aSLionel Sambuc   Opts.MergeAllConstants = !Args.hasArg(OPT_fno_merge_all_constants);
409f4a2713aSLionel Sambuc   Opts.NoCommon = Args.hasArg(OPT_fno_common);
410f4a2713aSLionel Sambuc   Opts.NoImplicitFloat = Args.hasArg(OPT_no_implicit_float);
411f4a2713aSLionel Sambuc   Opts.OptimizeSize = getOptimizationLevelSize(Args);
412f4a2713aSLionel Sambuc   Opts.SimplifyLibCalls = !(Args.hasArg(OPT_fno_builtin) ||
413f4a2713aSLionel Sambuc                             Args.hasArg(OPT_ffreestanding));
414f4a2713aSLionel Sambuc   Opts.UnrollLoops =
415f4a2713aSLionel Sambuc       Args.hasFlag(OPT_funroll_loops, OPT_fno_unroll_loops,
416f4a2713aSLionel Sambuc                    (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize));
417f4a2713aSLionel Sambuc   Opts.RerollLoops = Args.hasArg(OPT_freroll_loops);
418f4a2713aSLionel Sambuc 
419*0a6a1f1dSLionel Sambuc   Opts.DisableIntegratedAS = Args.hasArg(OPT_fno_integrated_as);
420f4a2713aSLionel Sambuc   Opts.Autolink = !Args.hasArg(OPT_fno_autolink);
421f4a2713aSLionel Sambuc   Opts.SampleProfileFile = Args.getLastArgValue(OPT_fprofile_sample_use_EQ);
422*0a6a1f1dSLionel Sambuc   Opts.ProfileInstrGenerate = Args.hasArg(OPT_fprofile_instr_generate);
423*0a6a1f1dSLionel Sambuc   Opts.InstrProfileInput = Args.getLastArgValue(OPT_fprofile_instr_use_EQ);
424*0a6a1f1dSLionel Sambuc   Opts.CoverageMapping = Args.hasArg(OPT_fcoverage_mapping);
425*0a6a1f1dSLionel Sambuc   Opts.DumpCoverageMapping = Args.hasArg(OPT_dump_coverage_mapping);
426f4a2713aSLionel Sambuc   Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose);
427f4a2713aSLionel Sambuc   Opts.ObjCAutoRefCountExceptions = Args.hasArg(OPT_fobjc_arc_exceptions);
428f4a2713aSLionel Sambuc   Opts.CUDAIsDevice = Args.hasArg(OPT_fcuda_is_device);
429f4a2713aSLionel Sambuc   Opts.CXAAtExit = !Args.hasArg(OPT_fno_use_cxa_atexit);
430f4a2713aSLionel Sambuc   Opts.CXXCtorDtorAliases = Args.hasArg(OPT_mconstructor_aliases);
431*0a6a1f1dSLionel Sambuc   Opts.CodeModel = getCodeModel(Args, Diags);
432f4a2713aSLionel Sambuc   Opts.DebugPass = Args.getLastArgValue(OPT_mdebug_pass);
433f4a2713aSLionel Sambuc   Opts.DisableFPElim = Args.hasArg(OPT_mdisable_fp_elim);
434f4a2713aSLionel Sambuc   Opts.DisableFree = Args.hasArg(OPT_disable_free);
435f4a2713aSLionel Sambuc   Opts.DisableTailCalls = Args.hasArg(OPT_mdisable_tail_calls);
436f4a2713aSLionel Sambuc   Opts.FloatABI = Args.getLastArgValue(OPT_mfloat_abi);
437f4a2713aSLionel Sambuc   Opts.LessPreciseFPMAD = Args.hasArg(OPT_cl_mad_enable);
438f4a2713aSLionel Sambuc   Opts.LimitFloatPrecision = Args.getLastArgValue(OPT_mlimit_float_precision);
439f4a2713aSLionel Sambuc   Opts.NoInfsFPMath = (Args.hasArg(OPT_menable_no_infinities) ||
440f4a2713aSLionel Sambuc                        Args.hasArg(OPT_cl_finite_math_only) ||
441f4a2713aSLionel Sambuc                        Args.hasArg(OPT_cl_fast_relaxed_math));
442f4a2713aSLionel Sambuc   Opts.NoNaNsFPMath = (Args.hasArg(OPT_menable_no_nans) ||
443*0a6a1f1dSLionel Sambuc                        Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
444f4a2713aSLionel Sambuc                        Args.hasArg(OPT_cl_finite_math_only) ||
445f4a2713aSLionel Sambuc                        Args.hasArg(OPT_cl_fast_relaxed_math));
446*0a6a1f1dSLionel Sambuc   Opts.NoSignedZeros = Args.hasArg(OPT_cl_no_signed_zeros);
447f4a2713aSLionel Sambuc   Opts.NoZeroInitializedInBSS = Args.hasArg(OPT_mno_zero_initialized_in_bss);
448f4a2713aSLionel Sambuc   Opts.BackendOptions = Args.getAllArgValues(OPT_backend_option);
449f4a2713aSLionel Sambuc   Opts.NumRegisterParameters = getLastArgIntValue(Args, OPT_mregparm, 0, Diags);
450f4a2713aSLionel Sambuc   Opts.NoGlobalMerge = Args.hasArg(OPT_mno_global_merge);
451f4a2713aSLionel Sambuc   Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
452*0a6a1f1dSLionel Sambuc   Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
453f4a2713aSLionel Sambuc   Opts.EnableSegmentedStacks = Args.hasArg(OPT_split_stacks);
454f4a2713aSLionel Sambuc   Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
455f4a2713aSLionel Sambuc   Opts.OmitLeafFramePointer = Args.hasArg(OPT_momit_leaf_frame_pointer);
456f4a2713aSLionel Sambuc   Opts.SaveTempLabels = Args.hasArg(OPT_msave_temp_labels);
457f4a2713aSLionel Sambuc   Opts.NoDwarfDirectoryAsm = Args.hasArg(OPT_fno_dwarf_directory_asm);
458f4a2713aSLionel Sambuc   Opts.SoftFloat = Args.hasArg(OPT_msoft_float);
459f4a2713aSLionel Sambuc   Opts.StrictEnums = Args.hasArg(OPT_fstrict_enums);
460f4a2713aSLionel Sambuc   Opts.UnsafeFPMath = Args.hasArg(OPT_menable_unsafe_fp_math) ||
461f4a2713aSLionel Sambuc                       Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
462f4a2713aSLionel Sambuc                       Args.hasArg(OPT_cl_fast_relaxed_math);
463f4a2713aSLionel Sambuc   Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
464f4a2713aSLionel Sambuc   Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
465*0a6a1f1dSLionel Sambuc   Opts.ThreadModel = Args.getLastArgValue(OPT_mthread_model, "posix");
466*0a6a1f1dSLionel Sambuc   if (Opts.ThreadModel != "posix" && Opts.ThreadModel != "single")
467*0a6a1f1dSLionel Sambuc     Diags.Report(diag::err_drv_invalid_value)
468*0a6a1f1dSLionel Sambuc         << Args.getLastArg(OPT_mthread_model)->getAsString(Args)
469*0a6a1f1dSLionel Sambuc         << Opts.ThreadModel;
470f4a2713aSLionel Sambuc   Opts.TrapFuncName = Args.getLastArgValue(OPT_ftrap_function_EQ);
471f4a2713aSLionel Sambuc   Opts.UseInitArray = Args.hasArg(OPT_fuse_init_array);
472f4a2713aSLionel Sambuc 
473f4a2713aSLionel Sambuc   Opts.FunctionSections = Args.hasFlag(OPT_ffunction_sections,
474f4a2713aSLionel Sambuc                                        OPT_fno_function_sections, false);
475f4a2713aSLionel Sambuc   Opts.DataSections = Args.hasFlag(OPT_fdata_sections,
476f4a2713aSLionel Sambuc                                    OPT_fno_data_sections, false);
477*0a6a1f1dSLionel Sambuc   Opts.MergeFunctions = Args.hasArg(OPT_fmerge_functions);
478f4a2713aSLionel Sambuc 
479f4a2713aSLionel Sambuc   Opts.VectorizeBB = Args.hasArg(OPT_vectorize_slp_aggressive);
480f4a2713aSLionel Sambuc   Opts.VectorizeLoop = Args.hasArg(OPT_vectorize_loops);
481f4a2713aSLionel Sambuc   Opts.VectorizeSLP = Args.hasArg(OPT_vectorize_slp);
482f4a2713aSLionel Sambuc 
483f4a2713aSLionel Sambuc   Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name);
484f4a2713aSLionel Sambuc   Opts.VerifyModule = !Args.hasArg(OPT_disable_llvm_verifier);
485f4a2713aSLionel Sambuc 
486f4a2713aSLionel Sambuc   Opts.DisableGCov = Args.hasArg(OPT_test_coverage);
487f4a2713aSLionel Sambuc   Opts.EmitGcovArcs = Args.hasArg(OPT_femit_coverage_data);
488f4a2713aSLionel Sambuc   Opts.EmitGcovNotes = Args.hasArg(OPT_femit_coverage_notes);
489f4a2713aSLionel Sambuc   if (Opts.EmitGcovArcs || Opts.EmitGcovNotes) {
490f4a2713aSLionel Sambuc     Opts.CoverageFile = Args.getLastArgValue(OPT_coverage_file);
491f4a2713aSLionel Sambuc     Opts.CoverageExtraChecksum = Args.hasArg(OPT_coverage_cfg_checksum);
492f4a2713aSLionel Sambuc     Opts.CoverageNoFunctionNamesInData =
493f4a2713aSLionel Sambuc         Args.hasArg(OPT_coverage_no_function_names_in_data);
494f4a2713aSLionel Sambuc     if (Args.hasArg(OPT_coverage_version_EQ)) {
495f4a2713aSLionel Sambuc       StringRef CoverageVersion = Args.getLastArgValue(OPT_coverage_version_EQ);
496f4a2713aSLionel Sambuc       if (CoverageVersion.size() != 4) {
497f4a2713aSLionel Sambuc         Diags.Report(diag::err_drv_invalid_value)
498f4a2713aSLionel Sambuc             << Args.getLastArg(OPT_coverage_version_EQ)->getAsString(Args)
499f4a2713aSLionel Sambuc             << CoverageVersion;
500f4a2713aSLionel Sambuc       } else {
501f4a2713aSLionel Sambuc         memcpy(Opts.CoverageVersion, CoverageVersion.data(), 4);
502f4a2713aSLionel Sambuc       }
503f4a2713aSLionel Sambuc     }
504f4a2713aSLionel Sambuc   }
505f4a2713aSLionel Sambuc 
506f4a2713aSLionel Sambuc   Opts.InstrumentFunctions = Args.hasArg(OPT_finstrument_functions);
507f4a2713aSLionel Sambuc   Opts.InstrumentForProfiling = Args.hasArg(OPT_pg);
508f4a2713aSLionel Sambuc   Opts.EmitOpenCLArgMetadata = Args.hasArg(OPT_cl_kernel_arg_info);
509*0a6a1f1dSLionel Sambuc   Opts.CompressDebugSections = Args.hasArg(OPT_compress_debug_sections);
510f4a2713aSLionel Sambuc   Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);
511f4a2713aSLionel Sambuc   Opts.LinkBitcodeFile = Args.getLastArgValue(OPT_mlink_bitcode_file);
512*0a6a1f1dSLionel Sambuc   Opts.SanitizeCoverage =
513*0a6a1f1dSLionel Sambuc       getLastArgIntValue(Args, OPT_fsanitize_coverage, 0, Diags);
514f4a2713aSLionel Sambuc   Opts.SanitizeMemoryTrackOrigins =
515*0a6a1f1dSLionel Sambuc       getLastArgIntValue(Args, OPT_fsanitize_memory_track_origins_EQ, 0, Diags);
516f4a2713aSLionel Sambuc   Opts.SanitizeUndefinedTrapOnError =
517f4a2713aSLionel Sambuc       Args.hasArg(OPT_fsanitize_undefined_trap_on_error);
518f4a2713aSLionel Sambuc   Opts.SSPBufferSize =
519f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_stack_protector_buffer_size, 8, Diags);
520f4a2713aSLionel Sambuc   Opts.StackRealignment = Args.hasArg(OPT_mstackrealign);
521f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_mstack_alignment)) {
522f4a2713aSLionel Sambuc     StringRef Val = A->getValue();
523f4a2713aSLionel Sambuc     unsigned StackAlignment = Opts.StackAlignment;
524f4a2713aSLionel Sambuc     Val.getAsInteger(10, StackAlignment);
525f4a2713aSLionel Sambuc     Opts.StackAlignment = StackAlignment;
526f4a2713aSLionel Sambuc   }
527f4a2713aSLionel Sambuc 
528f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) {
529f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
530f4a2713aSLionel Sambuc     unsigned Method = llvm::StringSwitch<unsigned>(Name)
531f4a2713aSLionel Sambuc       .Case("legacy", CodeGenOptions::Legacy)
532f4a2713aSLionel Sambuc       .Case("non-legacy", CodeGenOptions::NonLegacy)
533f4a2713aSLionel Sambuc       .Case("mixed", CodeGenOptions::Mixed)
534f4a2713aSLionel Sambuc       .Default(~0U);
535f4a2713aSLionel Sambuc     if (Method == ~0U) {
536f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
537f4a2713aSLionel Sambuc       Success = false;
538f4a2713aSLionel Sambuc     } else {
539f4a2713aSLionel Sambuc       Opts.setObjCDispatchMethod(
540f4a2713aSLionel Sambuc         static_cast<CodeGenOptions::ObjCDispatchMethodKind>(Method));
541f4a2713aSLionel Sambuc     }
542f4a2713aSLionel Sambuc   }
543f4a2713aSLionel Sambuc 
544f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_ftlsmodel_EQ)) {
545f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
546f4a2713aSLionel Sambuc     unsigned Model = llvm::StringSwitch<unsigned>(Name)
547f4a2713aSLionel Sambuc         .Case("global-dynamic", CodeGenOptions::GeneralDynamicTLSModel)
548f4a2713aSLionel Sambuc         .Case("local-dynamic", CodeGenOptions::LocalDynamicTLSModel)
549f4a2713aSLionel Sambuc         .Case("initial-exec", CodeGenOptions::InitialExecTLSModel)
550f4a2713aSLionel Sambuc         .Case("local-exec", CodeGenOptions::LocalExecTLSModel)
551f4a2713aSLionel Sambuc         .Default(~0U);
552f4a2713aSLionel Sambuc     if (Model == ~0U) {
553f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
554f4a2713aSLionel Sambuc       Success = false;
555f4a2713aSLionel Sambuc     } else {
556f4a2713aSLionel Sambuc       Opts.setDefaultTLSModel(static_cast<CodeGenOptions::TLSModel>(Model));
557f4a2713aSLionel Sambuc     }
558f4a2713aSLionel Sambuc   }
559f4a2713aSLionel Sambuc 
560f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_ffp_contract)) {
561f4a2713aSLionel Sambuc     StringRef Val = A->getValue();
562f4a2713aSLionel Sambuc     if (Val == "fast")
563f4a2713aSLionel Sambuc       Opts.setFPContractMode(CodeGenOptions::FPC_Fast);
564f4a2713aSLionel Sambuc     else if (Val == "on")
565f4a2713aSLionel Sambuc       Opts.setFPContractMode(CodeGenOptions::FPC_On);
566f4a2713aSLionel Sambuc     else if (Val == "off")
567f4a2713aSLionel Sambuc       Opts.setFPContractMode(CodeGenOptions::FPC_Off);
568f4a2713aSLionel Sambuc     else
569f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
570f4a2713aSLionel Sambuc   }
571f4a2713aSLionel Sambuc 
572f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_fpcc_struct_return, OPT_freg_struct_return)) {
573f4a2713aSLionel Sambuc     if (A->getOption().matches(OPT_fpcc_struct_return)) {
574f4a2713aSLionel Sambuc       Opts.setStructReturnConvention(CodeGenOptions::SRCK_OnStack);
575f4a2713aSLionel Sambuc     } else {
576f4a2713aSLionel Sambuc       assert(A->getOption().matches(OPT_freg_struct_return));
577f4a2713aSLionel Sambuc       Opts.setStructReturnConvention(CodeGenOptions::SRCK_InRegs);
578f4a2713aSLionel Sambuc     }
579f4a2713aSLionel Sambuc   }
580f4a2713aSLionel Sambuc 
581f4a2713aSLionel Sambuc   Opts.DependentLibraries = Args.getAllArgValues(OPT_dependent_lib);
582*0a6a1f1dSLionel Sambuc   bool NeedLocTracking = false;
583*0a6a1f1dSLionel Sambuc 
584*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_Rpass_EQ)) {
585*0a6a1f1dSLionel Sambuc     Opts.OptimizationRemarkPattern =
586*0a6a1f1dSLionel Sambuc         GenerateOptimizationRemarkRegex(Diags, Args, A);
587*0a6a1f1dSLionel Sambuc     NeedLocTracking = true;
588*0a6a1f1dSLionel Sambuc   }
589*0a6a1f1dSLionel Sambuc 
590*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_Rpass_missed_EQ)) {
591*0a6a1f1dSLionel Sambuc     Opts.OptimizationRemarkMissedPattern =
592*0a6a1f1dSLionel Sambuc         GenerateOptimizationRemarkRegex(Diags, Args, A);
593*0a6a1f1dSLionel Sambuc     NeedLocTracking = true;
594*0a6a1f1dSLionel Sambuc   }
595*0a6a1f1dSLionel Sambuc 
596*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_Rpass_analysis_EQ)) {
597*0a6a1f1dSLionel Sambuc     Opts.OptimizationRemarkAnalysisPattern =
598*0a6a1f1dSLionel Sambuc         GenerateOptimizationRemarkRegex(Diags, Args, A);
599*0a6a1f1dSLionel Sambuc     NeedLocTracking = true;
600*0a6a1f1dSLionel Sambuc   }
601*0a6a1f1dSLionel Sambuc 
602*0a6a1f1dSLionel Sambuc   // If the user requested to use a sample profile for PGO, then the
603*0a6a1f1dSLionel Sambuc   // backend will need to track source location information so the profile
604*0a6a1f1dSLionel Sambuc   // can be incorporated into the IR.
605*0a6a1f1dSLionel Sambuc   if (!Opts.SampleProfileFile.empty())
606*0a6a1f1dSLionel Sambuc     NeedLocTracking = true;
607*0a6a1f1dSLionel Sambuc 
608*0a6a1f1dSLionel Sambuc   // If the user requested a flag that requires source locations available in
609*0a6a1f1dSLionel Sambuc   // the backend, make sure that the backend tracks source location information.
610*0a6a1f1dSLionel Sambuc   if (NeedLocTracking && Opts.getDebugInfo() == CodeGenOptions::NoDebugInfo)
611*0a6a1f1dSLionel Sambuc     Opts.setDebugInfo(CodeGenOptions::LocTrackingOnly);
612*0a6a1f1dSLionel Sambuc 
613*0a6a1f1dSLionel Sambuc   Opts.RewriteMapFiles = Args.getAllArgValues(OPT_frewrite_map_file);
614*0a6a1f1dSLionel Sambuc 
615*0a6a1f1dSLionel Sambuc   // Parse -fsanitize-recover= arguments.
616*0a6a1f1dSLionel Sambuc   // FIXME: Report unrecoverable sanitizers incorrectly specified here.
617*0a6a1f1dSLionel Sambuc   parseSanitizerKinds("-fsanitize-recover=",
618*0a6a1f1dSLionel Sambuc                       Args.getAllArgValues(OPT_fsanitize_recover_EQ), Diags,
619*0a6a1f1dSLionel Sambuc                       Opts.SanitizeRecover);
620f4a2713aSLionel Sambuc 
621f4a2713aSLionel Sambuc   return Success;
622f4a2713aSLionel Sambuc }
623f4a2713aSLionel Sambuc 
ParseDependencyOutputArgs(DependencyOutputOptions & Opts,ArgList & Args)624f4a2713aSLionel Sambuc static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
625f4a2713aSLionel Sambuc                                       ArgList &Args) {
626f4a2713aSLionel Sambuc   using namespace options;
627f4a2713aSLionel Sambuc   Opts.OutputFile = Args.getLastArgValue(OPT_dependency_file);
628f4a2713aSLionel Sambuc   Opts.Targets = Args.getAllArgValues(OPT_MT);
629f4a2713aSLionel Sambuc   Opts.IncludeSystemHeaders = Args.hasArg(OPT_sys_header_deps);
630*0a6a1f1dSLionel Sambuc   Opts.IncludeModuleFiles = Args.hasArg(OPT_module_file_deps);
631f4a2713aSLionel Sambuc   Opts.UsePhonyTargets = Args.hasArg(OPT_MP);
632f4a2713aSLionel Sambuc   Opts.ShowHeaderIncludes = Args.hasArg(OPT_H);
633f4a2713aSLionel Sambuc   Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file);
634f4a2713aSLionel Sambuc   Opts.AddMissingHeaderDeps = Args.hasArg(OPT_MG);
635f4a2713aSLionel Sambuc   Opts.PrintShowIncludes = Args.hasArg(OPT_show_includes);
636f4a2713aSLionel Sambuc   Opts.DOTOutputFile = Args.getLastArgValue(OPT_dependency_dot);
637*0a6a1f1dSLionel Sambuc   Opts.ModuleDependencyOutputDir =
638*0a6a1f1dSLionel Sambuc       Args.getLastArgValue(OPT_module_dependency_dir);
639f4a2713aSLionel Sambuc }
640f4a2713aSLionel Sambuc 
ParseDiagnosticArgs(DiagnosticOptions & Opts,ArgList & Args,DiagnosticsEngine * Diags)641f4a2713aSLionel Sambuc bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
642f4a2713aSLionel Sambuc                                 DiagnosticsEngine *Diags) {
643f4a2713aSLionel Sambuc   using namespace options;
644f4a2713aSLionel Sambuc   bool Success = true;
645f4a2713aSLionel Sambuc 
646f4a2713aSLionel Sambuc   Opts.DiagnosticLogFile = Args.getLastArgValue(OPT_diagnostic_log_file);
647*0a6a1f1dSLionel Sambuc   if (Arg *A =
648*0a6a1f1dSLionel Sambuc           Args.getLastArg(OPT_diagnostic_serialized_file, OPT__serialize_diags))
649*0a6a1f1dSLionel Sambuc     Opts.DiagnosticSerializationFile = A->getValue();
650f4a2713aSLionel Sambuc   Opts.IgnoreWarnings = Args.hasArg(OPT_w);
651f4a2713aSLionel Sambuc   Opts.NoRewriteMacros = Args.hasArg(OPT_Wno_rewrite_macros);
652f4a2713aSLionel Sambuc   Opts.Pedantic = Args.hasArg(OPT_pedantic);
653f4a2713aSLionel Sambuc   Opts.PedanticErrors = Args.hasArg(OPT_pedantic_errors);
654f4a2713aSLionel Sambuc   Opts.ShowCarets = !Args.hasArg(OPT_fno_caret_diagnostics);
655f4a2713aSLionel Sambuc   Opts.ShowColors = Args.hasArg(OPT_fcolor_diagnostics);
656f4a2713aSLionel Sambuc   Opts.ShowColumn = Args.hasFlag(OPT_fshow_column,
657f4a2713aSLionel Sambuc                                  OPT_fno_show_column,
658f4a2713aSLionel Sambuc                                  /*Default=*/true);
659f4a2713aSLionel Sambuc   Opts.ShowFixits = !Args.hasArg(OPT_fno_diagnostics_fixit_info);
660f4a2713aSLionel Sambuc   Opts.ShowLocation = !Args.hasArg(OPT_fno_show_source_location);
661f4a2713aSLionel Sambuc   Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option);
662f4a2713aSLionel Sambuc 
663f4a2713aSLionel Sambuc   llvm::sys::Process::UseANSIEscapeCodes(Args.hasArg(OPT_fansi_escape_codes));
664f4a2713aSLionel Sambuc 
665f4a2713aSLionel Sambuc   // Default behavior is to not to show note include stacks.
666f4a2713aSLionel Sambuc   Opts.ShowNoteIncludeStack = false;
667f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_fdiagnostics_show_note_include_stack,
668f4a2713aSLionel Sambuc                                OPT_fno_diagnostics_show_note_include_stack))
669f4a2713aSLionel Sambuc     if (A->getOption().matches(OPT_fdiagnostics_show_note_include_stack))
670f4a2713aSLionel Sambuc       Opts.ShowNoteIncludeStack = true;
671f4a2713aSLionel Sambuc 
672f4a2713aSLionel Sambuc   StringRef ShowOverloads =
673f4a2713aSLionel Sambuc     Args.getLastArgValue(OPT_fshow_overloads_EQ, "all");
674f4a2713aSLionel Sambuc   if (ShowOverloads == "best")
675f4a2713aSLionel Sambuc     Opts.setShowOverloads(Ovl_Best);
676f4a2713aSLionel Sambuc   else if (ShowOverloads == "all")
677f4a2713aSLionel Sambuc     Opts.setShowOverloads(Ovl_All);
678f4a2713aSLionel Sambuc   else {
679f4a2713aSLionel Sambuc     Success = false;
680f4a2713aSLionel Sambuc     if (Diags)
681f4a2713aSLionel Sambuc       Diags->Report(diag::err_drv_invalid_value)
682f4a2713aSLionel Sambuc       << Args.getLastArg(OPT_fshow_overloads_EQ)->getAsString(Args)
683f4a2713aSLionel Sambuc       << ShowOverloads;
684f4a2713aSLionel Sambuc   }
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc   StringRef ShowCategory =
687f4a2713aSLionel Sambuc     Args.getLastArgValue(OPT_fdiagnostics_show_category, "none");
688f4a2713aSLionel Sambuc   if (ShowCategory == "none")
689f4a2713aSLionel Sambuc     Opts.ShowCategories = 0;
690f4a2713aSLionel Sambuc   else if (ShowCategory == "id")
691f4a2713aSLionel Sambuc     Opts.ShowCategories = 1;
692f4a2713aSLionel Sambuc   else if (ShowCategory == "name")
693f4a2713aSLionel Sambuc     Opts.ShowCategories = 2;
694f4a2713aSLionel Sambuc   else {
695f4a2713aSLionel Sambuc     Success = false;
696f4a2713aSLionel Sambuc     if (Diags)
697f4a2713aSLionel Sambuc       Diags->Report(diag::err_drv_invalid_value)
698f4a2713aSLionel Sambuc       << Args.getLastArg(OPT_fdiagnostics_show_category)->getAsString(Args)
699f4a2713aSLionel Sambuc       << ShowCategory;
700f4a2713aSLionel Sambuc   }
701f4a2713aSLionel Sambuc 
702f4a2713aSLionel Sambuc   StringRef Format =
703f4a2713aSLionel Sambuc     Args.getLastArgValue(OPT_fdiagnostics_format, "clang");
704f4a2713aSLionel Sambuc   if (Format == "clang")
705f4a2713aSLionel Sambuc     Opts.setFormat(DiagnosticOptions::Clang);
706f4a2713aSLionel Sambuc   else if (Format == "msvc")
707f4a2713aSLionel Sambuc     Opts.setFormat(DiagnosticOptions::Msvc);
708f4a2713aSLionel Sambuc   else if (Format == "msvc-fallback") {
709f4a2713aSLionel Sambuc     Opts.setFormat(DiagnosticOptions::Msvc);
710f4a2713aSLionel Sambuc     Opts.CLFallbackMode = true;
711f4a2713aSLionel Sambuc   } else if (Format == "vi")
712f4a2713aSLionel Sambuc     Opts.setFormat(DiagnosticOptions::Vi);
713f4a2713aSLionel Sambuc   else {
714f4a2713aSLionel Sambuc     Success = false;
715f4a2713aSLionel Sambuc     if (Diags)
716f4a2713aSLionel Sambuc       Diags->Report(diag::err_drv_invalid_value)
717f4a2713aSLionel Sambuc       << Args.getLastArg(OPT_fdiagnostics_format)->getAsString(Args)
718f4a2713aSLionel Sambuc       << Format;
719f4a2713aSLionel Sambuc   }
720f4a2713aSLionel Sambuc 
721f4a2713aSLionel Sambuc   Opts.ShowSourceRanges = Args.hasArg(OPT_fdiagnostics_print_source_range_info);
722f4a2713aSLionel Sambuc   Opts.ShowParseableFixits = Args.hasArg(OPT_fdiagnostics_parseable_fixits);
723f4a2713aSLionel Sambuc   Opts.ShowPresumedLoc = !Args.hasArg(OPT_fno_diagnostics_use_presumed_location);
724f4a2713aSLionel Sambuc   Opts.VerifyDiagnostics = Args.hasArg(OPT_verify);
725f4a2713aSLionel Sambuc   Opts.ElideType = !Args.hasArg(OPT_fno_elide_type);
726f4a2713aSLionel Sambuc   Opts.ShowTemplateTree = Args.hasArg(OPT_fdiagnostics_show_template_tree);
727f4a2713aSLionel Sambuc   Opts.ErrorLimit = getLastArgIntValue(Args, OPT_ferror_limit, 0, Diags);
728f4a2713aSLionel Sambuc   Opts.MacroBacktraceLimit =
729f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_fmacro_backtrace_limit,
730f4a2713aSLionel Sambuc                          DiagnosticOptions::DefaultMacroBacktraceLimit, Diags);
731f4a2713aSLionel Sambuc   Opts.TemplateBacktraceLimit = getLastArgIntValue(
732f4a2713aSLionel Sambuc       Args, OPT_ftemplate_backtrace_limit,
733f4a2713aSLionel Sambuc       DiagnosticOptions::DefaultTemplateBacktraceLimit, Diags);
734f4a2713aSLionel Sambuc   Opts.ConstexprBacktraceLimit = getLastArgIntValue(
735f4a2713aSLionel Sambuc       Args, OPT_fconstexpr_backtrace_limit,
736f4a2713aSLionel Sambuc       DiagnosticOptions::DefaultConstexprBacktraceLimit, Diags);
737*0a6a1f1dSLionel Sambuc   Opts.SpellCheckingLimit = getLastArgIntValue(
738*0a6a1f1dSLionel Sambuc       Args, OPT_fspell_checking_limit,
739*0a6a1f1dSLionel Sambuc       DiagnosticOptions::DefaultSpellCheckingLimit, Diags);
740f4a2713aSLionel Sambuc   Opts.TabStop = getLastArgIntValue(Args, OPT_ftabstop,
741f4a2713aSLionel Sambuc                                     DiagnosticOptions::DefaultTabStop, Diags);
742f4a2713aSLionel Sambuc   if (Opts.TabStop == 0 || Opts.TabStop > DiagnosticOptions::MaxTabStop) {
743f4a2713aSLionel Sambuc     Opts.TabStop = DiagnosticOptions::DefaultTabStop;
744f4a2713aSLionel Sambuc     if (Diags)
745f4a2713aSLionel Sambuc       Diags->Report(diag::warn_ignoring_ftabstop_value)
746f4a2713aSLionel Sambuc       << Opts.TabStop << DiagnosticOptions::DefaultTabStop;
747f4a2713aSLionel Sambuc   }
748f4a2713aSLionel Sambuc   Opts.MessageLength = getLastArgIntValue(Args, OPT_fmessage_length, 0, Diags);
749*0a6a1f1dSLionel Sambuc   addDiagnosticArgs(Args, OPT_W_Group, OPT_W_value_Group, Opts.Warnings);
750*0a6a1f1dSLionel Sambuc   addDiagnosticArgs(Args, OPT_R_Group, OPT_R_value_Group, Opts.Remarks);
751f4a2713aSLionel Sambuc 
752f4a2713aSLionel Sambuc   return Success;
753f4a2713aSLionel Sambuc }
754f4a2713aSLionel Sambuc 
ParseFileSystemArgs(FileSystemOptions & Opts,ArgList & Args)755f4a2713aSLionel Sambuc static void ParseFileSystemArgs(FileSystemOptions &Opts, ArgList &Args) {
756f4a2713aSLionel Sambuc   Opts.WorkingDir = Args.getLastArgValue(OPT_working_directory);
757f4a2713aSLionel Sambuc }
758f4a2713aSLionel Sambuc 
ParseFrontendArgs(FrontendOptions & Opts,ArgList & Args,DiagnosticsEngine & Diags)759f4a2713aSLionel Sambuc static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
760f4a2713aSLionel Sambuc                                    DiagnosticsEngine &Diags) {
761f4a2713aSLionel Sambuc   using namespace options;
762f4a2713aSLionel Sambuc   Opts.ProgramAction = frontend::ParseSyntaxOnly;
763f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_Action_Group)) {
764f4a2713aSLionel Sambuc     switch (A->getOption().getID()) {
765f4a2713aSLionel Sambuc     default:
766f4a2713aSLionel Sambuc       llvm_unreachable("Invalid option in group!");
767f4a2713aSLionel Sambuc     case OPT_ast_list:
768f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::ASTDeclList; break;
769f4a2713aSLionel Sambuc     case OPT_ast_dump:
770*0a6a1f1dSLionel Sambuc     case OPT_ast_dump_lookups:
771f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::ASTDump; break;
772f4a2713aSLionel Sambuc     case OPT_ast_print:
773f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::ASTPrint; break;
774f4a2713aSLionel Sambuc     case OPT_ast_view:
775f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::ASTView; break;
776f4a2713aSLionel Sambuc     case OPT_dump_raw_tokens:
777f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::DumpRawTokens; break;
778f4a2713aSLionel Sambuc     case OPT_dump_tokens:
779f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::DumpTokens; break;
780f4a2713aSLionel Sambuc     case OPT_S:
781f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitAssembly; break;
782f4a2713aSLionel Sambuc     case OPT_emit_llvm_bc:
783f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitBC; break;
784f4a2713aSLionel Sambuc     case OPT_emit_html:
785f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitHTML; break;
786f4a2713aSLionel Sambuc     case OPT_emit_llvm:
787f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitLLVM; break;
788f4a2713aSLionel Sambuc     case OPT_emit_llvm_only:
789f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitLLVMOnly; break;
790f4a2713aSLionel Sambuc     case OPT_emit_codegen_only:
791f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitCodeGenOnly; break;
792f4a2713aSLionel Sambuc     case OPT_emit_obj:
793f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::EmitObj; break;
794f4a2713aSLionel Sambuc     case OPT_fixit_EQ:
795f4a2713aSLionel Sambuc       Opts.FixItSuffix = A->getValue();
796f4a2713aSLionel Sambuc       // fall-through!
797f4a2713aSLionel Sambuc     case OPT_fixit:
798f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::FixIt; break;
799f4a2713aSLionel Sambuc     case OPT_emit_module:
800f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::GenerateModule; break;
801f4a2713aSLionel Sambuc     case OPT_emit_pch:
802f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::GeneratePCH; break;
803f4a2713aSLionel Sambuc     case OPT_emit_pth:
804f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::GeneratePTH; break;
805f4a2713aSLionel Sambuc     case OPT_init_only:
806f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::InitOnly; break;
807f4a2713aSLionel Sambuc     case OPT_fsyntax_only:
808f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::ParseSyntaxOnly; break;
809f4a2713aSLionel Sambuc     case OPT_module_file_info:
810f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::ModuleFileInfo; break;
811*0a6a1f1dSLionel Sambuc     case OPT_verify_pch:
812*0a6a1f1dSLionel Sambuc       Opts.ProgramAction = frontend::VerifyPCH; break;
813f4a2713aSLionel Sambuc     case OPT_print_decl_contexts:
814f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::PrintDeclContext; break;
815f4a2713aSLionel Sambuc     case OPT_print_preamble:
816f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::PrintPreamble; break;
817f4a2713aSLionel Sambuc     case OPT_E:
818f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::PrintPreprocessedInput; break;
819f4a2713aSLionel Sambuc     case OPT_rewrite_macros:
820f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::RewriteMacros; break;
821f4a2713aSLionel Sambuc     case OPT_rewrite_objc:
822f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::RewriteObjC; break;
823f4a2713aSLionel Sambuc     case OPT_rewrite_test:
824f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::RewriteTest; break;
825f4a2713aSLionel Sambuc     case OPT_analyze:
826f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::RunAnalysis; break;
827f4a2713aSLionel Sambuc     case OPT_migrate:
828f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::MigrateSource; break;
829f4a2713aSLionel Sambuc     case OPT_Eonly:
830f4a2713aSLionel Sambuc       Opts.ProgramAction = frontend::RunPreprocessorOnly; break;
831f4a2713aSLionel Sambuc     }
832f4a2713aSLionel Sambuc   }
833f4a2713aSLionel Sambuc 
834f4a2713aSLionel Sambuc   if (const Arg* A = Args.getLastArg(OPT_plugin)) {
835f4a2713aSLionel Sambuc     Opts.Plugins.push_back(A->getValue(0));
836f4a2713aSLionel Sambuc     Opts.ProgramAction = frontend::PluginAction;
837f4a2713aSLionel Sambuc     Opts.ActionName = A->getValue();
838f4a2713aSLionel Sambuc 
839f4a2713aSLionel Sambuc     for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg),
840f4a2713aSLionel Sambuc            end = Args.filtered_end(); it != end; ++it) {
841f4a2713aSLionel Sambuc       if ((*it)->getValue(0) == Opts.ActionName)
842f4a2713aSLionel Sambuc         Opts.PluginArgs.push_back((*it)->getValue(1));
843f4a2713aSLionel Sambuc     }
844f4a2713aSLionel Sambuc   }
845f4a2713aSLionel Sambuc 
846f4a2713aSLionel Sambuc   Opts.AddPluginActions = Args.getAllArgValues(OPT_add_plugin);
847f4a2713aSLionel Sambuc   Opts.AddPluginArgs.resize(Opts.AddPluginActions.size());
848f4a2713aSLionel Sambuc   for (int i = 0, e = Opts.AddPluginActions.size(); i != e; ++i) {
849f4a2713aSLionel Sambuc     for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg),
850f4a2713aSLionel Sambuc            end = Args.filtered_end(); it != end; ++it) {
851f4a2713aSLionel Sambuc       if ((*it)->getValue(0) == Opts.AddPluginActions[i])
852f4a2713aSLionel Sambuc         Opts.AddPluginArgs[i].push_back((*it)->getValue(1));
853f4a2713aSLionel Sambuc     }
854f4a2713aSLionel Sambuc   }
855f4a2713aSLionel Sambuc 
856f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_code_completion_at)) {
857f4a2713aSLionel Sambuc     Opts.CodeCompletionAt =
858f4a2713aSLionel Sambuc       ParsedSourceLocation::FromString(A->getValue());
859f4a2713aSLionel Sambuc     if (Opts.CodeCompletionAt.FileName.empty())
860f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
861f4a2713aSLionel Sambuc         << A->getAsString(Args) << A->getValue();
862f4a2713aSLionel Sambuc   }
863f4a2713aSLionel Sambuc   Opts.DisableFree = Args.hasArg(OPT_disable_free);
864f4a2713aSLionel Sambuc 
865f4a2713aSLionel Sambuc   Opts.OutputFile = Args.getLastArgValue(OPT_o);
866f4a2713aSLionel Sambuc   Opts.Plugins = Args.getAllArgValues(OPT_load);
867f4a2713aSLionel Sambuc   Opts.RelocatablePCH = Args.hasArg(OPT_relocatable_pch);
868f4a2713aSLionel Sambuc   Opts.ShowHelp = Args.hasArg(OPT_help);
869f4a2713aSLionel Sambuc   Opts.ShowStats = Args.hasArg(OPT_print_stats);
870f4a2713aSLionel Sambuc   Opts.ShowTimers = Args.hasArg(OPT_ftime_report);
871f4a2713aSLionel Sambuc   Opts.ShowVersion = Args.hasArg(OPT_version);
872f4a2713aSLionel Sambuc   Opts.ASTMergeFiles = Args.getAllArgValues(OPT_ast_merge);
873f4a2713aSLionel Sambuc   Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
874f4a2713aSLionel Sambuc   Opts.FixWhatYouCan = Args.hasArg(OPT_fix_what_you_can);
875f4a2713aSLionel Sambuc   Opts.FixOnlyWarnings = Args.hasArg(OPT_fix_only_warnings);
876f4a2713aSLionel Sambuc   Opts.FixAndRecompile = Args.hasArg(OPT_fixit_recompile);
877f4a2713aSLionel Sambuc   Opts.FixToTemporaries = Args.hasArg(OPT_fixit_to_temp);
878*0a6a1f1dSLionel Sambuc   Opts.ASTDumpDecls = Args.hasArg(OPT_ast_dump);
879f4a2713aSLionel Sambuc   Opts.ASTDumpFilter = Args.getLastArgValue(OPT_ast_dump_filter);
880f4a2713aSLionel Sambuc   Opts.ASTDumpLookups = Args.hasArg(OPT_ast_dump_lookups);
881f4a2713aSLionel Sambuc   Opts.UseGlobalModuleIndex = !Args.hasArg(OPT_fno_modules_global_index);
882f4a2713aSLionel Sambuc   Opts.GenerateGlobalModuleIndex = Opts.UseGlobalModuleIndex;
883*0a6a1f1dSLionel Sambuc   Opts.ModuleMapFiles = Args.getAllArgValues(OPT_fmodule_map_file);
884*0a6a1f1dSLionel Sambuc   Opts.ModuleFiles = Args.getAllArgValues(OPT_fmodule_file);
885f4a2713aSLionel Sambuc 
886f4a2713aSLionel Sambuc   Opts.CodeCompleteOpts.IncludeMacros
887f4a2713aSLionel Sambuc     = Args.hasArg(OPT_code_completion_macros);
888f4a2713aSLionel Sambuc   Opts.CodeCompleteOpts.IncludeCodePatterns
889f4a2713aSLionel Sambuc     = Args.hasArg(OPT_code_completion_patterns);
890f4a2713aSLionel Sambuc   Opts.CodeCompleteOpts.IncludeGlobals
891f4a2713aSLionel Sambuc     = !Args.hasArg(OPT_no_code_completion_globals);
892f4a2713aSLionel Sambuc   Opts.CodeCompleteOpts.IncludeBriefComments
893f4a2713aSLionel Sambuc     = Args.hasArg(OPT_code_completion_brief_comments);
894f4a2713aSLionel Sambuc 
895f4a2713aSLionel Sambuc   Opts.OverrideRecordLayoutsFile
896f4a2713aSLionel Sambuc     = Args.getLastArgValue(OPT_foverride_record_layout_EQ);
897f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_arcmt_check,
898f4a2713aSLionel Sambuc                                      OPT_arcmt_modify,
899f4a2713aSLionel Sambuc                                      OPT_arcmt_migrate)) {
900f4a2713aSLionel Sambuc     switch (A->getOption().getID()) {
901f4a2713aSLionel Sambuc     default:
902f4a2713aSLionel Sambuc       llvm_unreachable("missed a case");
903f4a2713aSLionel Sambuc     case OPT_arcmt_check:
904f4a2713aSLionel Sambuc       Opts.ARCMTAction = FrontendOptions::ARCMT_Check;
905f4a2713aSLionel Sambuc       break;
906f4a2713aSLionel Sambuc     case OPT_arcmt_modify:
907f4a2713aSLionel Sambuc       Opts.ARCMTAction = FrontendOptions::ARCMT_Modify;
908f4a2713aSLionel Sambuc       break;
909f4a2713aSLionel Sambuc     case OPT_arcmt_migrate:
910f4a2713aSLionel Sambuc       Opts.ARCMTAction = FrontendOptions::ARCMT_Migrate;
911f4a2713aSLionel Sambuc       break;
912f4a2713aSLionel Sambuc     }
913f4a2713aSLionel Sambuc   }
914f4a2713aSLionel Sambuc   Opts.MTMigrateDir = Args.getLastArgValue(OPT_mt_migrate_directory);
915f4a2713aSLionel Sambuc   Opts.ARCMTMigrateReportOut
916f4a2713aSLionel Sambuc     = Args.getLastArgValue(OPT_arcmt_migrate_report_output);
917f4a2713aSLionel Sambuc   Opts.ARCMTMigrateEmitARCErrors
918f4a2713aSLionel Sambuc     = Args.hasArg(OPT_arcmt_migrate_emit_arc_errors);
919f4a2713aSLionel Sambuc 
920f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_literals))
921f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Literals;
922f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_subscripting))
923f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Subscripting;
924*0a6a1f1dSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_property_dot_syntax))
925*0a6a1f1dSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_PropertyDotSyntax;
926f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_property))
927f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Property;
928f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_readonly_property))
929f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_ReadonlyProperty;
930f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_readwrite_property))
931f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_ReadwriteProperty;
932f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_annotation))
933f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Annotation;
934f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_returns_innerpointer_property))
935f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_ReturnsInnerPointerProperty;
936f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_instancetype))
937f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_Instancetype;
938f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_nsmacros))
939f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_NsMacros;
940f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_protocol_conformance))
941f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_ProtocolConformance;
942f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_atomic_property))
943f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_AtomicProperty;
944f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_ns_nonatomic_iosonly))
945f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty;
946*0a6a1f1dSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_designated_init))
947*0a6a1f1dSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_DesignatedInitializer;
948f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_objcmt_migrate_all))
949f4a2713aSLionel Sambuc     Opts.ObjCMTAction |= FrontendOptions::ObjCMT_MigrateDecls;
950f4a2713aSLionel Sambuc 
951*0a6a1f1dSLionel Sambuc   Opts.ObjCMTWhiteListPath = Args.getLastArgValue(OPT_objcmt_whitelist_dir_path);
952f4a2713aSLionel Sambuc 
953f4a2713aSLionel Sambuc   if (Opts.ARCMTAction != FrontendOptions::ARCMT_None &&
954f4a2713aSLionel Sambuc       Opts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
955f4a2713aSLionel Sambuc     Diags.Report(diag::err_drv_argument_not_allowed_with)
956f4a2713aSLionel Sambuc       << "ARC migration" << "ObjC migration";
957f4a2713aSLionel Sambuc   }
958f4a2713aSLionel Sambuc 
959f4a2713aSLionel Sambuc   InputKind DashX = IK_None;
960f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_x)) {
961f4a2713aSLionel Sambuc     DashX = llvm::StringSwitch<InputKind>(A->getValue())
962f4a2713aSLionel Sambuc       .Case("c", IK_C)
963f4a2713aSLionel Sambuc       .Case("cl", IK_OpenCL)
964f4a2713aSLionel Sambuc       .Case("cuda", IK_CUDA)
965f4a2713aSLionel Sambuc       .Case("c++", IK_CXX)
966f4a2713aSLionel Sambuc       .Case("objective-c", IK_ObjC)
967f4a2713aSLionel Sambuc       .Case("objective-c++", IK_ObjCXX)
968f4a2713aSLionel Sambuc       .Case("cpp-output", IK_PreprocessedC)
969f4a2713aSLionel Sambuc       .Case("assembler-with-cpp", IK_Asm)
970f4a2713aSLionel Sambuc       .Case("c++-cpp-output", IK_PreprocessedCXX)
971f4a2713aSLionel Sambuc       .Case("objective-c-cpp-output", IK_PreprocessedObjC)
972f4a2713aSLionel Sambuc       .Case("objc-cpp-output", IK_PreprocessedObjC)
973f4a2713aSLionel Sambuc       .Case("objective-c++-cpp-output", IK_PreprocessedObjCXX)
974f4a2713aSLionel Sambuc       .Case("objc++-cpp-output", IK_PreprocessedObjCXX)
975f4a2713aSLionel Sambuc       .Case("c-header", IK_C)
976f4a2713aSLionel Sambuc       .Case("cl-header", IK_OpenCL)
977f4a2713aSLionel Sambuc       .Case("objective-c-header", IK_ObjC)
978f4a2713aSLionel Sambuc       .Case("c++-header", IK_CXX)
979f4a2713aSLionel Sambuc       .Case("objective-c++-header", IK_ObjCXX)
980f4a2713aSLionel Sambuc       .Cases("ast", "pcm", IK_AST)
981f4a2713aSLionel Sambuc       .Case("ir", IK_LLVM_IR)
982f4a2713aSLionel Sambuc       .Default(IK_None);
983f4a2713aSLionel Sambuc     if (DashX == IK_None)
984f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
985f4a2713aSLionel Sambuc         << A->getAsString(Args) << A->getValue();
986f4a2713aSLionel Sambuc   }
987f4a2713aSLionel Sambuc 
988f4a2713aSLionel Sambuc   // '-' is the default input if none is given.
989f4a2713aSLionel Sambuc   std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT);
990f4a2713aSLionel Sambuc   Opts.Inputs.clear();
991f4a2713aSLionel Sambuc   if (Inputs.empty())
992f4a2713aSLionel Sambuc     Inputs.push_back("-");
993f4a2713aSLionel Sambuc   for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
994f4a2713aSLionel Sambuc     InputKind IK = DashX;
995f4a2713aSLionel Sambuc     if (IK == IK_None) {
996f4a2713aSLionel Sambuc       IK = FrontendOptions::getInputKindForExtension(
997f4a2713aSLionel Sambuc         StringRef(Inputs[i]).rsplit('.').second);
998f4a2713aSLionel Sambuc       // FIXME: Remove this hack.
999f4a2713aSLionel Sambuc       if (i == 0)
1000f4a2713aSLionel Sambuc         DashX = IK;
1001f4a2713aSLionel Sambuc     }
1002f4a2713aSLionel Sambuc     Opts.Inputs.push_back(FrontendInputFile(Inputs[i], IK));
1003f4a2713aSLionel Sambuc   }
1004f4a2713aSLionel Sambuc 
1005f4a2713aSLionel Sambuc   return DashX;
1006f4a2713aSLionel Sambuc }
1007f4a2713aSLionel Sambuc 
GetResourcesPath(const char * Argv0,void * MainAddr)1008f4a2713aSLionel Sambuc std::string CompilerInvocation::GetResourcesPath(const char *Argv0,
1009f4a2713aSLionel Sambuc                                                  void *MainAddr) {
1010*0a6a1f1dSLionel Sambuc   std::string ClangExecutable =
1011*0a6a1f1dSLionel Sambuc       llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
1012*0a6a1f1dSLionel Sambuc   StringRef Dir = llvm::sys::path::parent_path(ClangExecutable);
1013f4a2713aSLionel Sambuc 
1014*0a6a1f1dSLionel Sambuc   // Compute the path to the resource directory.
1015*0a6a1f1dSLionel Sambuc   StringRef ClangResourceDir(CLANG_RESOURCE_DIR);
1016*0a6a1f1dSLionel Sambuc   SmallString<128> P(Dir);
1017*0a6a1f1dSLionel Sambuc   if (ClangResourceDir != "") {
1018*0a6a1f1dSLionel Sambuc     llvm::sys::path::append(P, ClangResourceDir);
1019*0a6a1f1dSLionel Sambuc   } else {
1020*0a6a1f1dSLionel Sambuc     StringRef ClangLibdirSuffix(CLANG_LIBDIR_SUFFIX);
1021*0a6a1f1dSLionel Sambuc     llvm::sys::path::append(P, "..", Twine("lib") + ClangLibdirSuffix, "clang",
1022*0a6a1f1dSLionel Sambuc                             CLANG_VERSION_STRING);
1023f4a2713aSLionel Sambuc   }
1024f4a2713aSLionel Sambuc 
1025f4a2713aSLionel Sambuc   return P.str();
1026f4a2713aSLionel Sambuc }
1027f4a2713aSLionel Sambuc 
ParseHeaderSearchArgs(HeaderSearchOptions & Opts,ArgList & Args)1028f4a2713aSLionel Sambuc static void ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args) {
1029f4a2713aSLionel Sambuc   using namespace options;
1030f4a2713aSLionel Sambuc   Opts.Sysroot = Args.getLastArgValue(OPT_isysroot, "/");
1031f4a2713aSLionel Sambuc   Opts.Verbose = Args.hasArg(OPT_v);
1032f4a2713aSLionel Sambuc   Opts.UseBuiltinIncludes = !Args.hasArg(OPT_nobuiltininc);
1033f4a2713aSLionel Sambuc   Opts.UseStandardSystemIncludes = !Args.hasArg(OPT_nostdsysteminc);
1034f4a2713aSLionel Sambuc   Opts.UseStandardCXXIncludes = !Args.hasArg(OPT_nostdincxx);
1035f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_stdlib_EQ))
1036f4a2713aSLionel Sambuc     Opts.UseLibcxx = (strcmp(A->getValue(), "libc++") == 0);
1037f4a2713aSLionel Sambuc   Opts.ResourceDir = Args.getLastArgValue(OPT_resource_dir);
1038f4a2713aSLionel Sambuc   Opts.ModuleCachePath = Args.getLastArgValue(OPT_fmodules_cache_path);
1039*0a6a1f1dSLionel Sambuc   Opts.ModuleUserBuildPath = Args.getLastArgValue(OPT_fmodules_user_build_path);
1040f4a2713aSLionel Sambuc   Opts.DisableModuleHash = Args.hasArg(OPT_fdisable_module_hash);
1041f4a2713aSLionel Sambuc   // -fmodules implies -fmodule-maps
1042f4a2713aSLionel Sambuc   Opts.ModuleMaps = Args.hasArg(OPT_fmodule_maps) || Args.hasArg(OPT_fmodules);
1043*0a6a1f1dSLionel Sambuc   Opts.ModuleMapFileHomeIsCwd = Args.hasArg(OPT_fmodule_map_file_home_is_cwd);
1044f4a2713aSLionel Sambuc   Opts.ModuleCachePruneInterval =
1045f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_fmodules_prune_interval, 7 * 24 * 60 * 60);
1046f4a2713aSLionel Sambuc   Opts.ModuleCachePruneAfter =
1047f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_fmodules_prune_after, 31 * 24 * 60 * 60);
1048*0a6a1f1dSLionel Sambuc   Opts.ModulesValidateOncePerBuildSession =
1049*0a6a1f1dSLionel Sambuc       Args.hasArg(OPT_fmodules_validate_once_per_build_session);
1050*0a6a1f1dSLionel Sambuc   Opts.BuildSessionTimestamp =
1051*0a6a1f1dSLionel Sambuc       getLastArgUInt64Value(Args, OPT_fbuild_session_timestamp, 0);
1052*0a6a1f1dSLionel Sambuc   Opts.ModulesValidateSystemHeaders =
1053*0a6a1f1dSLionel Sambuc       Args.hasArg(OPT_fmodules_validate_system_headers);
1054*0a6a1f1dSLionel Sambuc 
1055f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_fmodules_ignore_macro),
1056f4a2713aSLionel Sambuc                     ie = Args.filtered_end();
1057f4a2713aSLionel Sambuc        it != ie; ++it) {
1058f4a2713aSLionel Sambuc     StringRef MacroDef = (*it)->getValue();
1059f4a2713aSLionel Sambuc     Opts.ModulesIgnoreMacros.insert(MacroDef.split('=').first);
1060f4a2713aSLionel Sambuc   }
1061f4a2713aSLionel Sambuc 
1062f4a2713aSLionel Sambuc   // Add -I..., -F..., and -index-header-map options in order.
1063f4a2713aSLionel Sambuc   bool IsIndexHeaderMap = false;
1064f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_I, OPT_F,
1065f4a2713aSLionel Sambuc                                              OPT_index_header_map),
1066f4a2713aSLionel Sambuc        ie = Args.filtered_end(); it != ie; ++it) {
1067f4a2713aSLionel Sambuc     if ((*it)->getOption().matches(OPT_index_header_map)) {
1068f4a2713aSLionel Sambuc       // -index-header-map applies to the next -I or -F.
1069f4a2713aSLionel Sambuc       IsIndexHeaderMap = true;
1070f4a2713aSLionel Sambuc       continue;
1071f4a2713aSLionel Sambuc     }
1072f4a2713aSLionel Sambuc 
1073f4a2713aSLionel Sambuc     frontend::IncludeDirGroup Group
1074f4a2713aSLionel Sambuc       = IsIndexHeaderMap? frontend::IndexHeaderMap : frontend::Angled;
1075f4a2713aSLionel Sambuc 
1076f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), Group,
1077f4a2713aSLionel Sambuc                  /*IsFramework=*/ (*it)->getOption().matches(OPT_F), true);
1078f4a2713aSLionel Sambuc     IsIndexHeaderMap = false;
1079f4a2713aSLionel Sambuc   }
1080f4a2713aSLionel Sambuc 
1081f4a2713aSLionel Sambuc   // Add -iprefix/-iwithprefix/-iwithprefixbefore options.
1082f4a2713aSLionel Sambuc   StringRef Prefix = ""; // FIXME: This isn't the correct default prefix.
1083f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_iprefix, OPT_iwithprefix,
1084f4a2713aSLionel Sambuc                                              OPT_iwithprefixbefore),
1085f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
1086f4a2713aSLionel Sambuc     const Arg *A = *it;
1087f4a2713aSLionel Sambuc     if (A->getOption().matches(OPT_iprefix))
1088f4a2713aSLionel Sambuc       Prefix = A->getValue();
1089f4a2713aSLionel Sambuc     else if (A->getOption().matches(OPT_iwithprefix))
1090f4a2713aSLionel Sambuc       Opts.AddPath(Prefix.str() + A->getValue(),
1091f4a2713aSLionel Sambuc                    frontend::After, false, true);
1092f4a2713aSLionel Sambuc     else
1093f4a2713aSLionel Sambuc       Opts.AddPath(Prefix.str() + A->getValue(),
1094f4a2713aSLionel Sambuc                    frontend::Angled, false, true);
1095f4a2713aSLionel Sambuc   }
1096f4a2713aSLionel Sambuc 
1097f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_idirafter),
1098f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it)
1099f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::After, false, true);
1100f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_iquote),
1101f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it)
1102f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::Quoted, false, true);
1103f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_isystem,
1104f4a2713aSLionel Sambuc          OPT_iwithsysroot), ie = Args.filtered_end(); it != ie; ++it)
1105f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::System, false,
1106f4a2713aSLionel Sambuc                  !(*it)->getOption().matches(OPT_iwithsysroot));
1107f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_iframework),
1108f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it)
1109f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::System, true, true);
1110f4a2713aSLionel Sambuc 
1111f4a2713aSLionel Sambuc   // Add the paths for the various language specific isystem flags.
1112f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_c_isystem),
1113f4a2713aSLionel Sambuc        ie = Args.filtered_end(); it != ie; ++it)
1114f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::CSystem, false, true);
1115f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_cxx_isystem),
1116f4a2713aSLionel Sambuc        ie = Args.filtered_end(); it != ie; ++it)
1117f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::CXXSystem, false, true);
1118f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_objc_isystem),
1119f4a2713aSLionel Sambuc        ie = Args.filtered_end(); it != ie; ++it)
1120f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::ObjCSystem, false,true);
1121f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_objcxx_isystem),
1122f4a2713aSLionel Sambuc        ie = Args.filtered_end(); it != ie; ++it)
1123f4a2713aSLionel Sambuc     Opts.AddPath((*it)->getValue(), frontend::ObjCXXSystem, false, true);
1124f4a2713aSLionel Sambuc 
1125f4a2713aSLionel Sambuc   // Add the internal paths from a driver that detects standard include paths.
1126f4a2713aSLionel Sambuc   for (arg_iterator I = Args.filtered_begin(OPT_internal_isystem,
1127f4a2713aSLionel Sambuc                                             OPT_internal_externc_isystem),
1128f4a2713aSLionel Sambuc                     E = Args.filtered_end();
1129f4a2713aSLionel Sambuc        I != E; ++I) {
1130f4a2713aSLionel Sambuc     frontend::IncludeDirGroup Group = frontend::System;
1131f4a2713aSLionel Sambuc     if ((*I)->getOption().matches(OPT_internal_externc_isystem))
1132f4a2713aSLionel Sambuc       Group = frontend::ExternCSystem;
1133f4a2713aSLionel Sambuc     Opts.AddPath((*I)->getValue(), Group, false, true);
1134f4a2713aSLionel Sambuc   }
1135f4a2713aSLionel Sambuc 
1136f4a2713aSLionel Sambuc   // Add the path prefixes which are implicitly treated as being system headers.
1137*0a6a1f1dSLionel Sambuc   for (arg_iterator I = Args.filtered_begin(OPT_system_header_prefix,
1138*0a6a1f1dSLionel Sambuc                                             OPT_no_system_header_prefix),
1139f4a2713aSLionel Sambuc                     E = Args.filtered_end();
1140f4a2713aSLionel Sambuc        I != E; ++I)
1141*0a6a1f1dSLionel Sambuc     Opts.AddSystemHeaderPrefix(
1142*0a6a1f1dSLionel Sambuc         (*I)->getValue(), (*I)->getOption().matches(OPT_system_header_prefix));
1143*0a6a1f1dSLionel Sambuc 
1144*0a6a1f1dSLionel Sambuc   for (arg_iterator I = Args.filtered_begin(OPT_ivfsoverlay),
1145*0a6a1f1dSLionel Sambuc        E = Args.filtered_end(); I != E; ++I)
1146*0a6a1f1dSLionel Sambuc     Opts.AddVFSOverlayFile((*I)->getValue());
1147f4a2713aSLionel Sambuc }
1148f4a2713aSLionel Sambuc 
setLangDefaults(LangOptions & Opts,InputKind IK,LangStandard::Kind LangStd)1149f4a2713aSLionel Sambuc void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,
1150f4a2713aSLionel Sambuc                                          LangStandard::Kind LangStd) {
1151f4a2713aSLionel Sambuc   // Set some properties which depend solely on the input kind; it would be nice
1152f4a2713aSLionel Sambuc   // to move these to the language standard, and have the driver resolve the
1153f4a2713aSLionel Sambuc   // input kind + language standard.
1154f4a2713aSLionel Sambuc   if (IK == IK_Asm) {
1155f4a2713aSLionel Sambuc     Opts.AsmPreprocessor = 1;
1156f4a2713aSLionel Sambuc   } else if (IK == IK_ObjC ||
1157f4a2713aSLionel Sambuc              IK == IK_ObjCXX ||
1158f4a2713aSLionel Sambuc              IK == IK_PreprocessedObjC ||
1159f4a2713aSLionel Sambuc              IK == IK_PreprocessedObjCXX) {
1160f4a2713aSLionel Sambuc     Opts.ObjC1 = Opts.ObjC2 = 1;
1161f4a2713aSLionel Sambuc   }
1162f4a2713aSLionel Sambuc 
1163f4a2713aSLionel Sambuc   if (LangStd == LangStandard::lang_unspecified) {
1164f4a2713aSLionel Sambuc     // Based on the base language, pick one.
1165f4a2713aSLionel Sambuc     switch (IK) {
1166f4a2713aSLionel Sambuc     case IK_None:
1167f4a2713aSLionel Sambuc     case IK_AST:
1168f4a2713aSLionel Sambuc     case IK_LLVM_IR:
1169f4a2713aSLionel Sambuc       llvm_unreachable("Invalid input kind!");
1170f4a2713aSLionel Sambuc     case IK_OpenCL:
1171f4a2713aSLionel Sambuc       LangStd = LangStandard::lang_opencl;
1172f4a2713aSLionel Sambuc       break;
1173f4a2713aSLionel Sambuc     case IK_CUDA:
1174f4a2713aSLionel Sambuc       LangStd = LangStandard::lang_cuda;
1175f4a2713aSLionel Sambuc       break;
1176f4a2713aSLionel Sambuc     case IK_Asm:
1177f4a2713aSLionel Sambuc     case IK_C:
1178f4a2713aSLionel Sambuc     case IK_PreprocessedC:
1179f4a2713aSLionel Sambuc     case IK_ObjC:
1180f4a2713aSLionel Sambuc     case IK_PreprocessedObjC:
1181*0a6a1f1dSLionel Sambuc       LangStd = LangStandard::lang_gnu11;
1182f4a2713aSLionel Sambuc       break;
1183f4a2713aSLionel Sambuc     case IK_CXX:
1184f4a2713aSLionel Sambuc     case IK_PreprocessedCXX:
1185f4a2713aSLionel Sambuc     case IK_ObjCXX:
1186f4a2713aSLionel Sambuc     case IK_PreprocessedObjCXX:
1187f4a2713aSLionel Sambuc       LangStd = LangStandard::lang_gnucxx98;
1188f4a2713aSLionel Sambuc       break;
1189f4a2713aSLionel Sambuc     }
1190f4a2713aSLionel Sambuc   }
1191f4a2713aSLionel Sambuc 
1192f4a2713aSLionel Sambuc   const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
1193f4a2713aSLionel Sambuc   Opts.LineComment = Std.hasLineComments();
1194f4a2713aSLionel Sambuc   Opts.C99 = Std.isC99();
1195f4a2713aSLionel Sambuc   Opts.C11 = Std.isC11();
1196f4a2713aSLionel Sambuc   Opts.CPlusPlus = Std.isCPlusPlus();
1197f4a2713aSLionel Sambuc   Opts.CPlusPlus11 = Std.isCPlusPlus11();
1198*0a6a1f1dSLionel Sambuc   Opts.CPlusPlus14 = Std.isCPlusPlus14();
1199*0a6a1f1dSLionel Sambuc   Opts.CPlusPlus1z = Std.isCPlusPlus1z();
1200f4a2713aSLionel Sambuc   Opts.Digraphs = Std.hasDigraphs();
1201f4a2713aSLionel Sambuc   Opts.GNUMode = Std.isGNUMode();
1202f4a2713aSLionel Sambuc   Opts.GNUInline = !Std.isC99();
1203f4a2713aSLionel Sambuc   Opts.HexFloats = Std.hasHexFloats();
1204f4a2713aSLionel Sambuc   Opts.ImplicitInt = Std.hasImplicitInt();
1205f4a2713aSLionel Sambuc 
1206f4a2713aSLionel Sambuc   // Set OpenCL Version.
1207*0a6a1f1dSLionel Sambuc   Opts.OpenCL = LangStd == LangStandard::lang_opencl || IK == IK_OpenCL;
1208*0a6a1f1dSLionel Sambuc   if (LangStd == LangStandard::lang_opencl)
1209f4a2713aSLionel Sambuc     Opts.OpenCLVersion = 100;
1210*0a6a1f1dSLionel Sambuc   else if (LangStd == LangStandard::lang_opencl11)
1211f4a2713aSLionel Sambuc     Opts.OpenCLVersion = 110;
1212*0a6a1f1dSLionel Sambuc   else if (LangStd == LangStandard::lang_opencl12)
1213f4a2713aSLionel Sambuc     Opts.OpenCLVersion = 120;
1214*0a6a1f1dSLionel Sambuc   else if (LangStd == LangStandard::lang_opencl20)
1215*0a6a1f1dSLionel Sambuc     Opts.OpenCLVersion = 200;
1216f4a2713aSLionel Sambuc 
1217f4a2713aSLionel Sambuc   // OpenCL has some additional defaults.
1218f4a2713aSLionel Sambuc   if (Opts.OpenCL) {
1219f4a2713aSLionel Sambuc     Opts.AltiVec = 0;
1220f4a2713aSLionel Sambuc     Opts.CXXOperatorNames = 1;
1221f4a2713aSLionel Sambuc     Opts.LaxVectorConversions = 0;
1222f4a2713aSLionel Sambuc     Opts.DefaultFPContract = 1;
1223f4a2713aSLionel Sambuc     Opts.NativeHalfType = 1;
1224f4a2713aSLionel Sambuc   }
1225f4a2713aSLionel Sambuc 
1226*0a6a1f1dSLionel Sambuc   Opts.CUDA = LangStd == LangStandard::lang_cuda || IK == IK_CUDA;
1227f4a2713aSLionel Sambuc 
1228f4a2713aSLionel Sambuc   // OpenCL and C++ both have bool, true, false keywords.
1229f4a2713aSLionel Sambuc   Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
1230f4a2713aSLionel Sambuc 
1231*0a6a1f1dSLionel Sambuc   // OpenCL has half keyword
1232*0a6a1f1dSLionel Sambuc   Opts.Half = Opts.OpenCL;
1233*0a6a1f1dSLionel Sambuc 
1234f4a2713aSLionel Sambuc   // C++ has wchar_t keyword.
1235f4a2713aSLionel Sambuc   Opts.WChar = Opts.CPlusPlus;
1236f4a2713aSLionel Sambuc 
1237f4a2713aSLionel Sambuc   Opts.GNUKeywords = Opts.GNUMode;
1238f4a2713aSLionel Sambuc   Opts.CXXOperatorNames = Opts.CPlusPlus;
1239f4a2713aSLionel Sambuc 
1240f4a2713aSLionel Sambuc   Opts.DollarIdents = !Opts.AsmPreprocessor;
1241f4a2713aSLionel Sambuc 
1242*0a6a1f1dSLionel Sambuc   // C++14 onwards has sized global deallocation functions.
1243*0a6a1f1dSLionel Sambuc   Opts.SizedDeallocation = Opts.CPlusPlus14;
1244f4a2713aSLionel Sambuc }
1245f4a2713aSLionel Sambuc 
1246f4a2713aSLionel Sambuc /// Attempt to parse a visibility value out of the given argument.
parseVisibility(Arg * arg,ArgList & args,DiagnosticsEngine & diags)1247f4a2713aSLionel Sambuc static Visibility parseVisibility(Arg *arg, ArgList &args,
1248f4a2713aSLionel Sambuc                                   DiagnosticsEngine &diags) {
1249f4a2713aSLionel Sambuc   StringRef value = arg->getValue();
1250f4a2713aSLionel Sambuc   if (value == "default") {
1251f4a2713aSLionel Sambuc     return DefaultVisibility;
1252f4a2713aSLionel Sambuc   } else if (value == "hidden") {
1253f4a2713aSLionel Sambuc     return HiddenVisibility;
1254f4a2713aSLionel Sambuc   } else if (value == "protected") {
1255f4a2713aSLionel Sambuc     // FIXME: diagnose if target does not support protected visibility
1256f4a2713aSLionel Sambuc     return ProtectedVisibility;
1257f4a2713aSLionel Sambuc   }
1258f4a2713aSLionel Sambuc 
1259f4a2713aSLionel Sambuc   diags.Report(diag::err_drv_invalid_value)
1260f4a2713aSLionel Sambuc     << arg->getAsString(args) << value;
1261f4a2713aSLionel Sambuc   return DefaultVisibility;
1262f4a2713aSLionel Sambuc }
1263f4a2713aSLionel Sambuc 
parseMSCVersion(ArgList & Args,DiagnosticsEngine & Diags)1264*0a6a1f1dSLionel Sambuc static unsigned parseMSCVersion(ArgList &Args, DiagnosticsEngine &Diags) {
1265*0a6a1f1dSLionel Sambuc   auto Arg = Args.getLastArg(OPT_fms_compatibility_version);
1266*0a6a1f1dSLionel Sambuc   if (!Arg)
1267*0a6a1f1dSLionel Sambuc     return 0;
1268*0a6a1f1dSLionel Sambuc 
1269*0a6a1f1dSLionel Sambuc   // The MSC versioning scheme involves four versioning components:
1270*0a6a1f1dSLionel Sambuc   //  - Major
1271*0a6a1f1dSLionel Sambuc   //  - Minor
1272*0a6a1f1dSLionel Sambuc   //  - Build
1273*0a6a1f1dSLionel Sambuc   //  - Patch
1274*0a6a1f1dSLionel Sambuc   //
1275*0a6a1f1dSLionel Sambuc   // We accept either the old style (_MSC_VER) value, or a _MSC_FULL_VER value.
1276*0a6a1f1dSLionel Sambuc   // Additionally, the value may be provided in the form of a more readable
1277*0a6a1f1dSLionel Sambuc   // MM.mm.bbbbb.pp version.
1278*0a6a1f1dSLionel Sambuc   //
1279*0a6a1f1dSLionel Sambuc   // Unfortunately, due to the bit-width limitations, we cannot currently encode
1280*0a6a1f1dSLionel Sambuc   // the value for the patch level.
1281*0a6a1f1dSLionel Sambuc 
1282*0a6a1f1dSLionel Sambuc   unsigned VC[4] = {0};
1283*0a6a1f1dSLionel Sambuc   StringRef Value = Arg->getValue();
1284*0a6a1f1dSLionel Sambuc   SmallVector<StringRef, 4> Components;
1285*0a6a1f1dSLionel Sambuc 
1286*0a6a1f1dSLionel Sambuc   Value.split(Components, ".", llvm::array_lengthof(VC));
1287*0a6a1f1dSLionel Sambuc   for (unsigned CI = 0,
1288*0a6a1f1dSLionel Sambuc                 CE = std::min(Components.size(), llvm::array_lengthof(VC));
1289*0a6a1f1dSLionel Sambuc        CI < CE; ++CI) {
1290*0a6a1f1dSLionel Sambuc     if (Components[CI].getAsInteger(10, VC[CI])) {
1291*0a6a1f1dSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
1292*0a6a1f1dSLionel Sambuc         << Arg->getAsString(Args) << Value;
1293*0a6a1f1dSLionel Sambuc       return 0;
1294*0a6a1f1dSLionel Sambuc     }
1295*0a6a1f1dSLionel Sambuc   }
1296*0a6a1f1dSLionel Sambuc 
1297*0a6a1f1dSLionel Sambuc   // FIXME we cannot encode the patch level
1298*0a6a1f1dSLionel Sambuc   return VC[0] * 10000000 + VC[1] * 100000 + VC[2];
1299*0a6a1f1dSLionel Sambuc }
1300*0a6a1f1dSLionel Sambuc 
ParseLangArgs(LangOptions & Opts,ArgList & Args,InputKind IK,DiagnosticsEngine & Diags)1301f4a2713aSLionel Sambuc static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
1302f4a2713aSLionel Sambuc                           DiagnosticsEngine &Diags) {
1303f4a2713aSLionel Sambuc   // FIXME: Cleanup per-file based stuff.
1304f4a2713aSLionel Sambuc   LangStandard::Kind LangStd = LangStandard::lang_unspecified;
1305f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_std_EQ)) {
1306f4a2713aSLionel Sambuc     LangStd = llvm::StringSwitch<LangStandard::Kind>(A->getValue())
1307f4a2713aSLionel Sambuc #define LANGSTANDARD(id, name, desc, features) \
1308f4a2713aSLionel Sambuc       .Case(name, LangStandard::lang_##id)
1309f4a2713aSLionel Sambuc #include "clang/Frontend/LangStandards.def"
1310f4a2713aSLionel Sambuc       .Default(LangStandard::lang_unspecified);
1311f4a2713aSLionel Sambuc     if (LangStd == LangStandard::lang_unspecified)
1312f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
1313f4a2713aSLionel Sambuc         << A->getAsString(Args) << A->getValue();
1314f4a2713aSLionel Sambuc     else {
1315*0a6a1f1dSLionel Sambuc       // Valid standard, check to make sure language and standard are
1316*0a6a1f1dSLionel Sambuc       // compatible.
1317f4a2713aSLionel Sambuc       const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
1318f4a2713aSLionel Sambuc       switch (IK) {
1319f4a2713aSLionel Sambuc       case IK_C:
1320f4a2713aSLionel Sambuc       case IK_ObjC:
1321f4a2713aSLionel Sambuc       case IK_PreprocessedC:
1322f4a2713aSLionel Sambuc       case IK_PreprocessedObjC:
1323f4a2713aSLionel Sambuc         if (!(Std.isC89() || Std.isC99()))
1324f4a2713aSLionel Sambuc           Diags.Report(diag::err_drv_argument_not_allowed_with)
1325f4a2713aSLionel Sambuc             << A->getAsString(Args) << "C/ObjC";
1326f4a2713aSLionel Sambuc         break;
1327f4a2713aSLionel Sambuc       case IK_CXX:
1328f4a2713aSLionel Sambuc       case IK_ObjCXX:
1329f4a2713aSLionel Sambuc       case IK_PreprocessedCXX:
1330f4a2713aSLionel Sambuc       case IK_PreprocessedObjCXX:
1331f4a2713aSLionel Sambuc         if (!Std.isCPlusPlus())
1332f4a2713aSLionel Sambuc           Diags.Report(diag::err_drv_argument_not_allowed_with)
1333f4a2713aSLionel Sambuc             << A->getAsString(Args) << "C++/ObjC++";
1334f4a2713aSLionel Sambuc         break;
1335f4a2713aSLionel Sambuc       case IK_OpenCL:
1336f4a2713aSLionel Sambuc         if (!Std.isC99())
1337f4a2713aSLionel Sambuc           Diags.Report(diag::err_drv_argument_not_allowed_with)
1338f4a2713aSLionel Sambuc             << A->getAsString(Args) << "OpenCL";
1339f4a2713aSLionel Sambuc         break;
1340f4a2713aSLionel Sambuc       case IK_CUDA:
1341f4a2713aSLionel Sambuc         if (!Std.isCPlusPlus())
1342f4a2713aSLionel Sambuc           Diags.Report(diag::err_drv_argument_not_allowed_with)
1343f4a2713aSLionel Sambuc             << A->getAsString(Args) << "CUDA";
1344f4a2713aSLionel Sambuc         break;
1345f4a2713aSLionel Sambuc       default:
1346f4a2713aSLionel Sambuc         break;
1347f4a2713aSLionel Sambuc       }
1348f4a2713aSLionel Sambuc     }
1349f4a2713aSLionel Sambuc   }
1350f4a2713aSLionel Sambuc 
1351f4a2713aSLionel Sambuc   // -cl-std only applies for OpenCL language standards.
1352f4a2713aSLionel Sambuc   // Override the -std option in this case.
1353f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_cl_std_EQ)) {
1354f4a2713aSLionel Sambuc     LangStandard::Kind OpenCLLangStd
1355f4a2713aSLionel Sambuc     = llvm::StringSwitch<LangStandard::Kind>(A->getValue())
1356f4a2713aSLionel Sambuc     .Case("CL", LangStandard::lang_opencl)
1357f4a2713aSLionel Sambuc     .Case("CL1.1", LangStandard::lang_opencl11)
1358f4a2713aSLionel Sambuc     .Case("CL1.2", LangStandard::lang_opencl12)
1359*0a6a1f1dSLionel Sambuc     .Case("CL2.0", LangStandard::lang_opencl20)
1360f4a2713aSLionel Sambuc     .Default(LangStandard::lang_unspecified);
1361f4a2713aSLionel Sambuc 
1362f4a2713aSLionel Sambuc     if (OpenCLLangStd == LangStandard::lang_unspecified) {
1363f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
1364f4a2713aSLionel Sambuc       << A->getAsString(Args) << A->getValue();
1365f4a2713aSLionel Sambuc     }
1366f4a2713aSLionel Sambuc     else
1367f4a2713aSLionel Sambuc       LangStd = OpenCLLangStd;
1368f4a2713aSLionel Sambuc   }
1369f4a2713aSLionel Sambuc 
1370f4a2713aSLionel Sambuc   CompilerInvocation::setLangDefaults(Opts, IK, LangStd);
1371f4a2713aSLionel Sambuc 
1372f4a2713aSLionel Sambuc   // We abuse '-f[no-]gnu-keywords' to force overriding all GNU-extension
1373f4a2713aSLionel Sambuc   // keywords. This behavior is provided by GCC's poorly named '-fasm' flag,
1374f4a2713aSLionel Sambuc   // while a subset (the non-C++ GNU keywords) is provided by GCC's
1375f4a2713aSLionel Sambuc   // '-fgnu-keywords'. Clang conflates the two for simplicity under the single
1376f4a2713aSLionel Sambuc   // name, as it doesn't seem a useful distinction.
1377f4a2713aSLionel Sambuc   Opts.GNUKeywords = Args.hasFlag(OPT_fgnu_keywords, OPT_fno_gnu_keywords,
1378f4a2713aSLionel Sambuc                                   Opts.GNUKeywords);
1379f4a2713aSLionel Sambuc 
1380f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fno_operator_names))
1381f4a2713aSLionel Sambuc     Opts.CXXOperatorNames = 0;
1382f4a2713aSLionel Sambuc 
1383*0a6a1f1dSLionel Sambuc   if (Args.hasArg(OPT_fcuda_is_device))
1384*0a6a1f1dSLionel Sambuc     Opts.CUDAIsDevice = 1;
1385*0a6a1f1dSLionel Sambuc 
1386f4a2713aSLionel Sambuc   if (Opts.ObjC1) {
1387f4a2713aSLionel Sambuc     if (Arg *arg = Args.getLastArg(OPT_fobjc_runtime_EQ)) {
1388f4a2713aSLionel Sambuc       StringRef value = arg->getValue();
1389f4a2713aSLionel Sambuc       if (Opts.ObjCRuntime.tryParse(value))
1390f4a2713aSLionel Sambuc         Diags.Report(diag::err_drv_unknown_objc_runtime) << value;
1391f4a2713aSLionel Sambuc     }
1392f4a2713aSLionel Sambuc 
1393f4a2713aSLionel Sambuc     if (Args.hasArg(OPT_fobjc_gc_only))
1394f4a2713aSLionel Sambuc       Opts.setGC(LangOptions::GCOnly);
1395f4a2713aSLionel Sambuc     else if (Args.hasArg(OPT_fobjc_gc))
1396f4a2713aSLionel Sambuc       Opts.setGC(LangOptions::HybridGC);
1397f4a2713aSLionel Sambuc     else if (Args.hasArg(OPT_fobjc_arc)) {
1398f4a2713aSLionel Sambuc       Opts.ObjCAutoRefCount = 1;
1399f4a2713aSLionel Sambuc       if (!Opts.ObjCRuntime.allowsARC())
1400f4a2713aSLionel Sambuc         Diags.Report(diag::err_arc_unsupported_on_runtime);
1401f4a2713aSLionel Sambuc 
1402f4a2713aSLionel Sambuc       // Only set ObjCARCWeak if ARC is enabled.
1403f4a2713aSLionel Sambuc       if (Args.hasArg(OPT_fobjc_runtime_has_weak))
1404f4a2713aSLionel Sambuc         Opts.ObjCARCWeak = 1;
1405f4a2713aSLionel Sambuc       else
1406f4a2713aSLionel Sambuc         Opts.ObjCARCWeak = Opts.ObjCRuntime.allowsWeak();
1407f4a2713aSLionel Sambuc     }
1408f4a2713aSLionel Sambuc 
1409f4a2713aSLionel Sambuc     if (Args.hasArg(OPT_fno_objc_infer_related_result_type))
1410f4a2713aSLionel Sambuc       Opts.ObjCInferRelatedResultType = 0;
1411f4a2713aSLionel Sambuc 
1412f4a2713aSLionel Sambuc     if (Args.hasArg(OPT_fobjc_subscripting_legacy_runtime))
1413f4a2713aSLionel Sambuc       Opts.ObjCSubscriptingLegacyRuntime =
1414f4a2713aSLionel Sambuc         (Opts.ObjCRuntime.getKind() == ObjCRuntime::FragileMacOSX);
1415f4a2713aSLionel Sambuc   }
1416f4a2713aSLionel Sambuc 
1417f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fgnu89_inline))
1418f4a2713aSLionel Sambuc     Opts.GNUInline = 1;
1419f4a2713aSLionel Sambuc 
1420f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fapple_kext)) {
1421f4a2713aSLionel Sambuc     if (!Opts.CPlusPlus)
1422f4a2713aSLionel Sambuc       Diags.Report(diag::warn_c_kext);
1423f4a2713aSLionel Sambuc     else
1424f4a2713aSLionel Sambuc       Opts.AppleKext = 1;
1425f4a2713aSLionel Sambuc   }
1426f4a2713aSLionel Sambuc 
1427f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_print_ivar_layout))
1428f4a2713aSLionel Sambuc     Opts.ObjCGCBitmapPrint = 1;
1429f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fno_constant_cfstrings))
1430f4a2713aSLionel Sambuc     Opts.NoConstantCFStrings = 1;
1431f4a2713aSLionel Sambuc 
1432f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_faltivec))
1433f4a2713aSLionel Sambuc     Opts.AltiVec = 1;
1434f4a2713aSLionel Sambuc 
1435f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_pthread))
1436f4a2713aSLionel Sambuc     Opts.POSIXThreads = 1;
1437f4a2713aSLionel Sambuc 
1438f4a2713aSLionel Sambuc   // The value-visibility mode defaults to "default".
1439f4a2713aSLionel Sambuc   if (Arg *visOpt = Args.getLastArg(OPT_fvisibility)) {
1440f4a2713aSLionel Sambuc     Opts.setValueVisibilityMode(parseVisibility(visOpt, Args, Diags));
1441f4a2713aSLionel Sambuc   } else {
1442f4a2713aSLionel Sambuc     Opts.setValueVisibilityMode(DefaultVisibility);
1443f4a2713aSLionel Sambuc   }
1444f4a2713aSLionel Sambuc 
1445f4a2713aSLionel Sambuc   // The type-visibility mode defaults to the value-visibility mode.
1446f4a2713aSLionel Sambuc   if (Arg *typeVisOpt = Args.getLastArg(OPT_ftype_visibility)) {
1447f4a2713aSLionel Sambuc     Opts.setTypeVisibilityMode(parseVisibility(typeVisOpt, Args, Diags));
1448f4a2713aSLionel Sambuc   } else {
1449f4a2713aSLionel Sambuc     Opts.setTypeVisibilityMode(Opts.getValueVisibilityMode());
1450f4a2713aSLionel Sambuc   }
1451f4a2713aSLionel Sambuc 
1452f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fvisibility_inlines_hidden))
1453f4a2713aSLionel Sambuc     Opts.InlineVisibilityHidden = 1;
1454f4a2713aSLionel Sambuc 
1455f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_ftrapv)) {
1456f4a2713aSLionel Sambuc     Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping);
1457f4a2713aSLionel Sambuc     // Set the handler, if one is specified.
1458f4a2713aSLionel Sambuc     Opts.OverflowHandler =
1459f4a2713aSLionel Sambuc         Args.getLastArgValue(OPT_ftrapv_handler);
1460f4a2713aSLionel Sambuc   }
1461f4a2713aSLionel Sambuc   else if (Args.hasArg(OPT_fwrapv))
1462f4a2713aSLionel Sambuc     Opts.setSignedOverflowBehavior(LangOptions::SOB_Defined);
1463f4a2713aSLionel Sambuc 
1464*0a6a1f1dSLionel Sambuc   Opts.MSVCCompat = Args.hasArg(OPT_fms_compatibility);
1465*0a6a1f1dSLionel Sambuc   Opts.MicrosoftExt = Opts.MSVCCompat || Args.hasArg(OPT_fms_extensions);
1466*0a6a1f1dSLionel Sambuc   Opts.AsmBlocks = Args.hasArg(OPT_fasm_blocks) || Opts.MicrosoftExt;
1467*0a6a1f1dSLionel Sambuc   Opts.MSCompatibilityVersion = parseMSCVersion(Args, Diags);
1468*0a6a1f1dSLionel Sambuc 
1469*0a6a1f1dSLionel Sambuc   // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
1470*0a6a1f1dSLionel Sambuc   // is specified, or -std is set to a conforming mode.
1471*0a6a1f1dSLionel Sambuc   // Trigraphs are disabled by default in c++1z onwards.
1472*0a6a1f1dSLionel Sambuc   Opts.Trigraphs = !Opts.GNUMode && !Opts.MSVCCompat && !Opts.CPlusPlus1z;
1473*0a6a1f1dSLionel Sambuc   Opts.Trigraphs =
1474*0a6a1f1dSLionel Sambuc       Args.hasFlag(OPT_ftrigraphs, OPT_fno_trigraphs, Opts.Trigraphs);
1475f4a2713aSLionel Sambuc 
1476f4a2713aSLionel Sambuc   Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
1477f4a2713aSLionel Sambuc                                    OPT_fno_dollars_in_identifiers,
1478f4a2713aSLionel Sambuc                                    Opts.DollarIdents);
1479f4a2713aSLionel Sambuc   Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
1480*0a6a1f1dSLionel Sambuc   Opts.VtorDispMode = getLastArgIntValue(Args, OPT_vtordisp_mode_EQ, 1, Diags);
1481f4a2713aSLionel Sambuc   Opts.Borland = Args.hasArg(OPT_fborland_extensions);
1482f4a2713aSLionel Sambuc   Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
1483f4a2713aSLionel Sambuc   Opts.ConstStrings = Args.hasFlag(OPT_fconst_strings, OPT_fno_const_strings,
1484f4a2713aSLionel Sambuc                                    Opts.ConstStrings);
1485f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fno_lax_vector_conversions))
1486f4a2713aSLionel Sambuc     Opts.LaxVectorConversions = 0;
1487f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_fno_threadsafe_statics))
1488f4a2713aSLionel Sambuc     Opts.ThreadsafeStatics = 0;
1489f4a2713aSLionel Sambuc   Opts.Exceptions = Args.hasArg(OPT_fexceptions);
1490f4a2713aSLionel Sambuc   Opts.ObjCExceptions = Args.hasArg(OPT_fobjc_exceptions);
1491f4a2713aSLionel Sambuc   Opts.CXXExceptions = Args.hasArg(OPT_fcxx_exceptions);
1492f4a2713aSLionel Sambuc   Opts.SjLjExceptions = Args.hasArg(OPT_fsjlj_exceptions);
1493f4a2713aSLionel Sambuc   Opts.TraditionalCPP = Args.hasArg(OPT_traditional_cpp);
1494f4a2713aSLionel Sambuc 
1495f4a2713aSLionel Sambuc   Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
1496*0a6a1f1dSLionel Sambuc   Opts.RTTIData = Opts.RTTI && !Args.hasArg(OPT_fno_rtti_data);
1497f4a2713aSLionel Sambuc   Opts.Blocks = Args.hasArg(OPT_fblocks);
1498f4a2713aSLionel Sambuc   Opts.BlocksRuntimeOptional = Args.hasArg(OPT_fblocks_runtime_optional);
1499f4a2713aSLionel Sambuc   Opts.Modules = Args.hasArg(OPT_fmodules);
1500*0a6a1f1dSLionel Sambuc   Opts.ModulesStrictDeclUse = Args.hasArg(OPT_fmodules_strict_decluse);
1501*0a6a1f1dSLionel Sambuc   Opts.ModulesDeclUse =
1502*0a6a1f1dSLionel Sambuc       Args.hasArg(OPT_fmodules_decluse) || Opts.ModulesStrictDeclUse;
1503*0a6a1f1dSLionel Sambuc   Opts.ModulesSearchAll = Opts.Modules &&
1504*0a6a1f1dSLionel Sambuc     !Args.hasArg(OPT_fno_modules_search_all) &&
1505*0a6a1f1dSLionel Sambuc     Args.hasArg(OPT_fmodules_search_all);
1506*0a6a1f1dSLionel Sambuc   Opts.ModulesErrorRecovery = !Args.hasArg(OPT_fno_modules_error_recovery);
1507*0a6a1f1dSLionel Sambuc   Opts.ModulesImplicitMaps = Args.hasFlag(OPT_fmodules_implicit_maps,
1508*0a6a1f1dSLionel Sambuc                                           OPT_fno_modules_implicit_maps, true);
1509f4a2713aSLionel Sambuc   Opts.CharIsSigned = Opts.OpenCL || !Args.hasArg(OPT_fno_signed_char);
1510f4a2713aSLionel Sambuc   Opts.WChar = Opts.CPlusPlus && !Args.hasArg(OPT_fno_wchar);
1511*0a6a1f1dSLionel Sambuc   Opts.ShortWChar = Args.hasFlag(OPT_fshort_wchar, OPT_fno_short_wchar, false);
1512f4a2713aSLionel Sambuc   Opts.ShortEnums = Args.hasArg(OPT_fshort_enums);
1513f4a2713aSLionel Sambuc   Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
1514f4a2713aSLionel Sambuc   Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
1515f4a2713aSLionel Sambuc   Opts.NoMathBuiltin = Args.hasArg(OPT_fno_math_builtin);
1516f4a2713aSLionel Sambuc   Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
1517f4a2713aSLionel Sambuc   Opts.SizedDeallocation |= Args.hasArg(OPT_fsized_deallocation);
1518f4a2713aSLionel Sambuc   Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
1519f4a2713aSLionel Sambuc   Opts.AccessControl = !Args.hasArg(OPT_fno_access_control);
1520f4a2713aSLionel Sambuc   Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
1521f4a2713aSLionel Sambuc   Opts.MathErrno = !Opts.OpenCL && Args.hasArg(OPT_fmath_errno);
1522f4a2713aSLionel Sambuc   Opts.InstantiationDepth =
1523f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_ftemplate_depth, 256, Diags);
1524f4a2713aSLionel Sambuc   Opts.ArrowDepth =
1525f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_foperator_arrow_depth, 256, Diags);
1526f4a2713aSLionel Sambuc   Opts.ConstexprCallDepth =
1527f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_fconstexpr_depth, 512, Diags);
1528f4a2713aSLionel Sambuc   Opts.ConstexprStepLimit =
1529f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_fconstexpr_steps, 1048576, Diags);
1530f4a2713aSLionel Sambuc   Opts.BracketDepth = getLastArgIntValue(Args, OPT_fbracket_depth, 256, Diags);
1531f4a2713aSLionel Sambuc   Opts.DelayedTemplateParsing = Args.hasArg(OPT_fdelayed_template_parsing);
1532f4a2713aSLionel Sambuc   Opts.NumLargeByValueCopy =
1533f4a2713aSLionel Sambuc       getLastArgIntValue(Args, OPT_Wlarge_by_value_copy_EQ, 0, Diags);
1534f4a2713aSLionel Sambuc   Opts.MSBitfields = Args.hasArg(OPT_mms_bitfields);
1535f4a2713aSLionel Sambuc   Opts.ObjCConstantStringClass =
1536f4a2713aSLionel Sambuc     Args.getLastArgValue(OPT_fconstant_string_class);
1537f4a2713aSLionel Sambuc   Opts.ObjCDefaultSynthProperties =
1538f4a2713aSLionel Sambuc     !Args.hasArg(OPT_disable_objc_default_synthesize_properties);
1539f4a2713aSLionel Sambuc   Opts.EncodeExtendedBlockSig =
1540f4a2713aSLionel Sambuc     Args.hasArg(OPT_fencode_extended_block_signature);
1541f4a2713aSLionel Sambuc   Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
1542f4a2713aSLionel Sambuc   Opts.PackStruct = getLastArgIntValue(Args, OPT_fpack_struct_EQ, 0, Diags);
1543*0a6a1f1dSLionel Sambuc   Opts.MaxTypeAlign = getLastArgIntValue(Args, OPT_fmax_type_align_EQ, 0, Diags);
1544f4a2713aSLionel Sambuc   Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
1545f4a2713aSLionel Sambuc   Opts.PIELevel = getLastArgIntValue(Args, OPT_pie_level, 0, Diags);
1546f4a2713aSLionel Sambuc   Opts.Static = Args.hasArg(OPT_static_define);
1547f4a2713aSLionel Sambuc   Opts.DumpRecordLayoutsSimple = Args.hasArg(OPT_fdump_record_layouts_simple);
1548f4a2713aSLionel Sambuc   Opts.DumpRecordLayouts = Opts.DumpRecordLayoutsSimple
1549f4a2713aSLionel Sambuc                         || Args.hasArg(OPT_fdump_record_layouts);
1550f4a2713aSLionel Sambuc   Opts.DumpVTableLayouts = Args.hasArg(OPT_fdump_vtable_layouts);
1551f4a2713aSLionel Sambuc   Opts.SpellChecking = !Args.hasArg(OPT_fno_spell_checking);
1552f4a2713aSLionel Sambuc   Opts.NoBitFieldTypeAlign = Args.hasArg(OPT_fno_bitfield_type_align);
1553f4a2713aSLionel Sambuc   Opts.SinglePrecisionConstants = Args.hasArg(OPT_cl_single_precision_constant);
1554f4a2713aSLionel Sambuc   Opts.FastRelaxedMath = Args.hasArg(OPT_cl_fast_relaxed_math);
1555f4a2713aSLionel Sambuc   Opts.MRTD = Args.hasArg(OPT_mrtd);
1556f4a2713aSLionel Sambuc   Opts.HexagonQdsp6Compat = Args.hasArg(OPT_mqdsp6_compat);
1557f4a2713aSLionel Sambuc   Opts.FakeAddressSpaceMap = Args.hasArg(OPT_ffake_address_space_map);
1558f4a2713aSLionel Sambuc   Opts.ParseUnknownAnytype = Args.hasArg(OPT_funknown_anytype);
1559f4a2713aSLionel Sambuc   Opts.DebuggerSupport = Args.hasArg(OPT_fdebugger_support);
1560f4a2713aSLionel Sambuc   Opts.DebuggerCastResultToId = Args.hasArg(OPT_fdebugger_cast_result_to_id);
1561f4a2713aSLionel Sambuc   Opts.DebuggerObjCLiteral = Args.hasArg(OPT_fdebugger_objc_literal);
1562f4a2713aSLionel Sambuc   Opts.ApplePragmaPack = Args.hasArg(OPT_fapple_pragma_pack);
1563f4a2713aSLionel Sambuc   Opts.CurrentModule = Args.getLastArgValue(OPT_fmodule_name);
1564*0a6a1f1dSLionel Sambuc   Opts.ImplementationOfModule =
1565*0a6a1f1dSLionel Sambuc       Args.getLastArgValue(OPT_fmodule_implementation_of);
1566*0a6a1f1dSLionel Sambuc   Opts.NativeHalfType = Opts.NativeHalfType;
1567*0a6a1f1dSLionel Sambuc   Opts.HalfArgsAndReturns = Args.hasArg(OPT_fallow_half_arguments_and_returns);
1568*0a6a1f1dSLionel Sambuc 
1569*0a6a1f1dSLionel Sambuc   if (!Opts.CurrentModule.empty() && !Opts.ImplementationOfModule.empty() &&
1570*0a6a1f1dSLionel Sambuc       Opts.CurrentModule != Opts.ImplementationOfModule) {
1571*0a6a1f1dSLionel Sambuc     Diags.Report(diag::err_conflicting_module_names)
1572*0a6a1f1dSLionel Sambuc         << Opts.CurrentModule << Opts.ImplementationOfModule;
1573*0a6a1f1dSLionel Sambuc   }
1574f4a2713aSLionel Sambuc 
1575f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_faddress_space_map_mangling_EQ)) {
1576f4a2713aSLionel Sambuc     switch (llvm::StringSwitch<unsigned>(A->getValue())
1577f4a2713aSLionel Sambuc       .Case("target", LangOptions::ASMM_Target)
1578f4a2713aSLionel Sambuc       .Case("no", LangOptions::ASMM_Off)
1579f4a2713aSLionel Sambuc       .Case("yes", LangOptions::ASMM_On)
1580f4a2713aSLionel Sambuc       .Default(255)) {
1581f4a2713aSLionel Sambuc     default:
1582f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
1583f4a2713aSLionel Sambuc         << "-faddress-space-map-mangling=" << A->getValue();
1584f4a2713aSLionel Sambuc       break;
1585f4a2713aSLionel Sambuc     case LangOptions::ASMM_Target:
1586f4a2713aSLionel Sambuc       Opts.setAddressSpaceMapMangling(LangOptions::ASMM_Target);
1587f4a2713aSLionel Sambuc       break;
1588f4a2713aSLionel Sambuc     case LangOptions::ASMM_On:
1589f4a2713aSLionel Sambuc       Opts.setAddressSpaceMapMangling(LangOptions::ASMM_On);
1590f4a2713aSLionel Sambuc       break;
1591f4a2713aSLionel Sambuc     case LangOptions::ASMM_Off:
1592f4a2713aSLionel Sambuc       Opts.setAddressSpaceMapMangling(LangOptions::ASMM_Off);
1593f4a2713aSLionel Sambuc       break;
1594f4a2713aSLionel Sambuc     }
1595f4a2713aSLionel Sambuc   }
1596f4a2713aSLionel Sambuc 
1597*0a6a1f1dSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_fms_memptr_rep_EQ)) {
1598*0a6a1f1dSLionel Sambuc     LangOptions::PragmaMSPointersToMembersKind InheritanceModel =
1599*0a6a1f1dSLionel Sambuc         llvm::StringSwitch<LangOptions::PragmaMSPointersToMembersKind>(
1600*0a6a1f1dSLionel Sambuc             A->getValue())
1601*0a6a1f1dSLionel Sambuc             .Case("single",
1602*0a6a1f1dSLionel Sambuc                   LangOptions::PPTMK_FullGeneralitySingleInheritance)
1603*0a6a1f1dSLionel Sambuc             .Case("multiple",
1604*0a6a1f1dSLionel Sambuc                   LangOptions::PPTMK_FullGeneralityMultipleInheritance)
1605*0a6a1f1dSLionel Sambuc             .Case("virtual",
1606*0a6a1f1dSLionel Sambuc                   LangOptions::PPTMK_FullGeneralityVirtualInheritance)
1607*0a6a1f1dSLionel Sambuc             .Default(LangOptions::PPTMK_BestCase);
1608*0a6a1f1dSLionel Sambuc     if (InheritanceModel == LangOptions::PPTMK_BestCase)
1609*0a6a1f1dSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value)
1610*0a6a1f1dSLionel Sambuc           << "-fms-memptr-rep=" << A->getValue();
1611*0a6a1f1dSLionel Sambuc 
1612*0a6a1f1dSLionel Sambuc     Opts.setMSPointerToMemberRepresentationMethod(InheritanceModel);
1613*0a6a1f1dSLionel Sambuc   }
1614*0a6a1f1dSLionel Sambuc 
1615*0a6a1f1dSLionel Sambuc   // Check if -fopenmp= is specified.
1616*0a6a1f1dSLionel Sambuc   if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
1617*0a6a1f1dSLionel Sambuc     Opts.OpenMP = llvm::StringSwitch<bool>(A->getValue())
1618*0a6a1f1dSLionel Sambuc         .Case("libiomp5", true)
1619*0a6a1f1dSLionel Sambuc         .Default(false);
1620*0a6a1f1dSLionel Sambuc   }
1621f4a2713aSLionel Sambuc 
1622f4a2713aSLionel Sambuc   // Record whether the __DEPRECATED define was requested.
1623f4a2713aSLionel Sambuc   Opts.Deprecated = Args.hasFlag(OPT_fdeprecated_macro,
1624f4a2713aSLionel Sambuc                                  OPT_fno_deprecated_macro,
1625f4a2713aSLionel Sambuc                                  Opts.Deprecated);
1626f4a2713aSLionel Sambuc 
1627f4a2713aSLionel Sambuc   // FIXME: Eliminate this dependency.
1628f4a2713aSLionel Sambuc   unsigned Opt = getOptimizationLevel(Args, IK, Diags),
1629f4a2713aSLionel Sambuc        OptSize = getOptimizationLevelSize(Args);
1630f4a2713aSLionel Sambuc   Opts.Optimize = Opt != 0;
1631f4a2713aSLionel Sambuc   Opts.OptimizeSize = OptSize != 0;
1632f4a2713aSLionel Sambuc 
1633f4a2713aSLionel Sambuc   // This is the __NO_INLINE__ define, which just depends on things like the
1634f4a2713aSLionel Sambuc   // optimization level and -fno-inline, not actually whether the backend has
1635f4a2713aSLionel Sambuc   // inlining enabled.
1636f4a2713aSLionel Sambuc   Opts.NoInlineDefine = !Opt || Args.hasArg(OPT_fno_inline);
1637f4a2713aSLionel Sambuc 
1638*0a6a1f1dSLionel Sambuc   Opts.FastMath = Args.hasArg(OPT_ffast_math) ||
1639*0a6a1f1dSLionel Sambuc       Args.hasArg(OPT_cl_fast_relaxed_math);
1640*0a6a1f1dSLionel Sambuc   Opts.FiniteMathOnly = Args.hasArg(OPT_ffinite_math_only) ||
1641*0a6a1f1dSLionel Sambuc       Args.hasArg(OPT_cl_finite_math_only) ||
1642*0a6a1f1dSLionel Sambuc       Args.hasArg(OPT_cl_fast_relaxed_math);
1643f4a2713aSLionel Sambuc 
1644f4a2713aSLionel Sambuc   Opts.RetainCommentsFromSystemHeaders =
1645f4a2713aSLionel Sambuc       Args.hasArg(OPT_fretain_comments_from_system_headers);
1646f4a2713aSLionel Sambuc 
1647f4a2713aSLionel Sambuc   unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
1648f4a2713aSLionel Sambuc   switch (SSP) {
1649f4a2713aSLionel Sambuc   default:
1650f4a2713aSLionel Sambuc     Diags.Report(diag::err_drv_invalid_value)
1651f4a2713aSLionel Sambuc       << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
1652f4a2713aSLionel Sambuc     break;
1653f4a2713aSLionel Sambuc   case 0: Opts.setStackProtector(LangOptions::SSPOff); break;
1654f4a2713aSLionel Sambuc   case 1: Opts.setStackProtector(LangOptions::SSPOn);  break;
1655*0a6a1f1dSLionel Sambuc   case 2: Opts.setStackProtector(LangOptions::SSPStrong); break;
1656*0a6a1f1dSLionel Sambuc   case 3: Opts.setStackProtector(LangOptions::SSPReq); break;
1657f4a2713aSLionel Sambuc   }
1658f4a2713aSLionel Sambuc 
1659f4a2713aSLionel Sambuc   // Parse -fsanitize= arguments.
1660*0a6a1f1dSLionel Sambuc   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
1661*0a6a1f1dSLionel Sambuc                       Diags, Opts.Sanitize);
1662*0a6a1f1dSLionel Sambuc   // -fsanitize-address-field-padding=N has to be a LangOpt, parse it here.
1663*0a6a1f1dSLionel Sambuc   Opts.SanitizeAddressFieldPadding =
1664*0a6a1f1dSLionel Sambuc       getLastArgIntValue(Args, OPT_fsanitize_address_field_padding, 0, Diags);
1665*0a6a1f1dSLionel Sambuc   Opts.SanitizerBlacklistFile = Args.getLastArgValue(OPT_fsanitize_blacklist);
1666f4a2713aSLionel Sambuc }
1667f4a2713aSLionel Sambuc 
ParsePreprocessorArgs(PreprocessorOptions & Opts,ArgList & Args,FileManager & FileMgr,DiagnosticsEngine & Diags)1668f4a2713aSLionel Sambuc static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
1669f4a2713aSLionel Sambuc                                   FileManager &FileMgr,
1670f4a2713aSLionel Sambuc                                   DiagnosticsEngine &Diags) {
1671f4a2713aSLionel Sambuc   using namespace options;
1672f4a2713aSLionel Sambuc   Opts.ImplicitPCHInclude = Args.getLastArgValue(OPT_include_pch);
1673f4a2713aSLionel Sambuc   Opts.ImplicitPTHInclude = Args.getLastArgValue(OPT_include_pth);
1674f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_token_cache))
1675f4a2713aSLionel Sambuc       Opts.TokenCache = A->getValue();
1676f4a2713aSLionel Sambuc   else
1677f4a2713aSLionel Sambuc     Opts.TokenCache = Opts.ImplicitPTHInclude;
1678f4a2713aSLionel Sambuc   Opts.UsePredefines = !Args.hasArg(OPT_undef);
1679f4a2713aSLionel Sambuc   Opts.DetailedRecord = Args.hasArg(OPT_detailed_preprocessing_record);
1680f4a2713aSLionel Sambuc   Opts.DisablePCHValidation = Args.hasArg(OPT_fno_validate_pch);
1681f4a2713aSLionel Sambuc 
1682f4a2713aSLionel Sambuc   Opts.DumpDeserializedPCHDecls = Args.hasArg(OPT_dump_deserialized_pch_decls);
1683f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_error_on_deserialized_pch_decl),
1684f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
1685f4a2713aSLionel Sambuc     const Arg *A = *it;
1686f4a2713aSLionel Sambuc     Opts.DeserializedPCHDeclsToErrorOn.insert(A->getValue());
1687f4a2713aSLionel Sambuc   }
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc   if (const Arg *A = Args.getLastArg(OPT_preamble_bytes_EQ)) {
1690f4a2713aSLionel Sambuc     StringRef Value(A->getValue());
1691f4a2713aSLionel Sambuc     size_t Comma = Value.find(',');
1692f4a2713aSLionel Sambuc     unsigned Bytes = 0;
1693f4a2713aSLionel Sambuc     unsigned EndOfLine = 0;
1694f4a2713aSLionel Sambuc 
1695f4a2713aSLionel Sambuc     if (Comma == StringRef::npos ||
1696f4a2713aSLionel Sambuc         Value.substr(0, Comma).getAsInteger(10, Bytes) ||
1697f4a2713aSLionel Sambuc         Value.substr(Comma + 1).getAsInteger(10, EndOfLine))
1698f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_preamble_format);
1699f4a2713aSLionel Sambuc     else {
1700f4a2713aSLionel Sambuc       Opts.PrecompiledPreambleBytes.first = Bytes;
1701f4a2713aSLionel Sambuc       Opts.PrecompiledPreambleBytes.second = (EndOfLine != 0);
1702f4a2713aSLionel Sambuc     }
1703f4a2713aSLionel Sambuc   }
1704f4a2713aSLionel Sambuc 
1705f4a2713aSLionel Sambuc   // Add macros from the command line.
1706f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_D, OPT_U),
1707f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
1708f4a2713aSLionel Sambuc     if ((*it)->getOption().matches(OPT_D))
1709f4a2713aSLionel Sambuc       Opts.addMacroDef((*it)->getValue());
1710f4a2713aSLionel Sambuc     else
1711f4a2713aSLionel Sambuc       Opts.addMacroUndef((*it)->getValue());
1712f4a2713aSLionel Sambuc   }
1713f4a2713aSLionel Sambuc 
1714f4a2713aSLionel Sambuc   Opts.MacroIncludes = Args.getAllArgValues(OPT_imacros);
1715f4a2713aSLionel Sambuc 
1716f4a2713aSLionel Sambuc   // Add the ordered list of -includes.
1717f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_include),
1718f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
1719f4a2713aSLionel Sambuc     const Arg *A = *it;
1720f4a2713aSLionel Sambuc     Opts.Includes.push_back(A->getValue());
1721f4a2713aSLionel Sambuc   }
1722f4a2713aSLionel Sambuc 
1723f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_chain_include),
1724f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
1725f4a2713aSLionel Sambuc     const Arg *A = *it;
1726f4a2713aSLionel Sambuc     Opts.ChainedIncludes.push_back(A->getValue());
1727f4a2713aSLionel Sambuc   }
1728f4a2713aSLionel Sambuc 
1729f4a2713aSLionel Sambuc   // Include 'altivec.h' if -faltivec option present
1730f4a2713aSLionel Sambuc   if (Args.hasArg(OPT_faltivec))
1731f4a2713aSLionel Sambuc     Opts.Includes.push_back("altivec.h");
1732f4a2713aSLionel Sambuc 
1733f4a2713aSLionel Sambuc   for (arg_iterator it = Args.filtered_begin(OPT_remap_file),
1734f4a2713aSLionel Sambuc          ie = Args.filtered_end(); it != ie; ++it) {
1735f4a2713aSLionel Sambuc     const Arg *A = *it;
1736f4a2713aSLionel Sambuc     std::pair<StringRef,StringRef> Split =
1737f4a2713aSLionel Sambuc       StringRef(A->getValue()).split(';');
1738f4a2713aSLionel Sambuc 
1739f4a2713aSLionel Sambuc     if (Split.second.empty()) {
1740f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_remap_file) << A->getAsString(Args);
1741f4a2713aSLionel Sambuc       continue;
1742f4a2713aSLionel Sambuc     }
1743f4a2713aSLionel Sambuc 
1744f4a2713aSLionel Sambuc     Opts.addRemappedFile(Split.first, Split.second);
1745f4a2713aSLionel Sambuc   }
1746f4a2713aSLionel Sambuc 
1747f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(OPT_fobjc_arc_cxxlib_EQ)) {
1748f4a2713aSLionel Sambuc     StringRef Name = A->getValue();
1749f4a2713aSLionel Sambuc     unsigned Library = llvm::StringSwitch<unsigned>(Name)
1750f4a2713aSLionel Sambuc       .Case("libc++", ARCXX_libcxx)
1751f4a2713aSLionel Sambuc       .Case("libstdc++", ARCXX_libstdcxx)
1752f4a2713aSLionel Sambuc       .Case("none", ARCXX_nolib)
1753f4a2713aSLionel Sambuc       .Default(~0U);
1754f4a2713aSLionel Sambuc     if (Library == ~0U)
1755f4a2713aSLionel Sambuc       Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
1756f4a2713aSLionel Sambuc     else
1757f4a2713aSLionel Sambuc       Opts.ObjCXXARCStandardLibrary = (ObjCXXARCStandardLibraryKind)Library;
1758f4a2713aSLionel Sambuc   }
1759f4a2713aSLionel Sambuc }
1760f4a2713aSLionel Sambuc 
ParsePreprocessorOutputArgs(PreprocessorOutputOptions & Opts,ArgList & Args,frontend::ActionKind Action)1761f4a2713aSLionel Sambuc static void ParsePreprocessorOutputArgs(PreprocessorOutputOptions &Opts,
1762f4a2713aSLionel Sambuc                                         ArgList &Args,
1763f4a2713aSLionel Sambuc                                         frontend::ActionKind Action) {
1764f4a2713aSLionel Sambuc   using namespace options;
1765f4a2713aSLionel Sambuc 
1766f4a2713aSLionel Sambuc   switch (Action) {
1767f4a2713aSLionel Sambuc   case frontend::ASTDeclList:
1768f4a2713aSLionel Sambuc   case frontend::ASTDump:
1769f4a2713aSLionel Sambuc   case frontend::ASTPrint:
1770f4a2713aSLionel Sambuc   case frontend::ASTView:
1771f4a2713aSLionel Sambuc   case frontend::EmitAssembly:
1772f4a2713aSLionel Sambuc   case frontend::EmitBC:
1773f4a2713aSLionel Sambuc   case frontend::EmitHTML:
1774f4a2713aSLionel Sambuc   case frontend::EmitLLVM:
1775f4a2713aSLionel Sambuc   case frontend::EmitLLVMOnly:
1776f4a2713aSLionel Sambuc   case frontend::EmitCodeGenOnly:
1777f4a2713aSLionel Sambuc   case frontend::EmitObj:
1778f4a2713aSLionel Sambuc   case frontend::FixIt:
1779f4a2713aSLionel Sambuc   case frontend::GenerateModule:
1780f4a2713aSLionel Sambuc   case frontend::GeneratePCH:
1781f4a2713aSLionel Sambuc   case frontend::GeneratePTH:
1782f4a2713aSLionel Sambuc   case frontend::ParseSyntaxOnly:
1783f4a2713aSLionel Sambuc   case frontend::ModuleFileInfo:
1784*0a6a1f1dSLionel Sambuc   case frontend::VerifyPCH:
1785f4a2713aSLionel Sambuc   case frontend::PluginAction:
1786f4a2713aSLionel Sambuc   case frontend::PrintDeclContext:
1787f4a2713aSLionel Sambuc   case frontend::RewriteObjC:
1788f4a2713aSLionel Sambuc   case frontend::RewriteTest:
1789f4a2713aSLionel Sambuc   case frontend::RunAnalysis:
1790f4a2713aSLionel Sambuc   case frontend::MigrateSource:
1791f4a2713aSLionel Sambuc     Opts.ShowCPP = 0;
1792f4a2713aSLionel Sambuc     break;
1793f4a2713aSLionel Sambuc 
1794f4a2713aSLionel Sambuc   case frontend::DumpRawTokens:
1795f4a2713aSLionel Sambuc   case frontend::DumpTokens:
1796f4a2713aSLionel Sambuc   case frontend::InitOnly:
1797f4a2713aSLionel Sambuc   case frontend::PrintPreamble:
1798f4a2713aSLionel Sambuc   case frontend::PrintPreprocessedInput:
1799f4a2713aSLionel Sambuc   case frontend::RewriteMacros:
1800f4a2713aSLionel Sambuc   case frontend::RunPreprocessorOnly:
1801f4a2713aSLionel Sambuc     Opts.ShowCPP = !Args.hasArg(OPT_dM);
1802f4a2713aSLionel Sambuc     break;
1803f4a2713aSLionel Sambuc   }
1804f4a2713aSLionel Sambuc 
1805f4a2713aSLionel Sambuc   Opts.ShowComments = Args.hasArg(OPT_C);
1806f4a2713aSLionel Sambuc   Opts.ShowLineMarkers = !Args.hasArg(OPT_P);
1807f4a2713aSLionel Sambuc   Opts.ShowMacroComments = Args.hasArg(OPT_CC);
1808f4a2713aSLionel Sambuc   Opts.ShowMacros = Args.hasArg(OPT_dM) || Args.hasArg(OPT_dD);
1809f4a2713aSLionel Sambuc   Opts.RewriteIncludes = Args.hasArg(OPT_frewrite_includes);
1810f4a2713aSLionel Sambuc }
1811f4a2713aSLionel Sambuc 
ParseTargetArgs(TargetOptions & Opts,ArgList & Args)1812f4a2713aSLionel Sambuc static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) {
1813f4a2713aSLionel Sambuc   using namespace options;
1814f4a2713aSLionel Sambuc   Opts.ABI = Args.getLastArgValue(OPT_target_abi);
1815f4a2713aSLionel Sambuc   Opts.CPU = Args.getLastArgValue(OPT_target_cpu);
1816f4a2713aSLionel Sambuc   Opts.FPMath = Args.getLastArgValue(OPT_mfpmath);
1817f4a2713aSLionel Sambuc   Opts.FeaturesAsWritten = Args.getAllArgValues(OPT_target_feature);
1818f4a2713aSLionel Sambuc   Opts.LinkerVersion = Args.getLastArgValue(OPT_target_linker_version);
1819f4a2713aSLionel Sambuc   Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
1820f4a2713aSLionel Sambuc 
1821f4a2713aSLionel Sambuc   // Use the default target triple if unspecified.
1822f4a2713aSLionel Sambuc   if (Opts.Triple.empty())
1823f4a2713aSLionel Sambuc     Opts.Triple = llvm::sys::getDefaultTargetTriple();
1824f4a2713aSLionel Sambuc }
1825f4a2713aSLionel Sambuc 
CreateFromArgs(CompilerInvocation & Res,const char * const * ArgBegin,const char * const * ArgEnd,DiagnosticsEngine & Diags)1826f4a2713aSLionel Sambuc bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
1827f4a2713aSLionel Sambuc                                         const char *const *ArgBegin,
1828f4a2713aSLionel Sambuc                                         const char *const *ArgEnd,
1829f4a2713aSLionel Sambuc                                         DiagnosticsEngine &Diags) {
1830f4a2713aSLionel Sambuc   bool Success = true;
1831f4a2713aSLionel Sambuc 
1832f4a2713aSLionel Sambuc   // Parse the arguments.
1833*0a6a1f1dSLionel Sambuc   std::unique_ptr<OptTable> Opts(createDriverOptTable());
1834f4a2713aSLionel Sambuc   const unsigned IncludedFlagsBitmask = options::CC1Option;
1835f4a2713aSLionel Sambuc   unsigned MissingArgIndex, MissingArgCount;
1836*0a6a1f1dSLionel Sambuc   std::unique_ptr<InputArgList> Args(
1837f4a2713aSLionel Sambuc       Opts->ParseArgs(ArgBegin, ArgEnd, MissingArgIndex, MissingArgCount,
1838f4a2713aSLionel Sambuc                       IncludedFlagsBitmask));
1839f4a2713aSLionel Sambuc 
1840f4a2713aSLionel Sambuc   // Check for missing argument error.
1841f4a2713aSLionel Sambuc   if (MissingArgCount) {
1842f4a2713aSLionel Sambuc     Diags.Report(diag::err_drv_missing_argument)
1843f4a2713aSLionel Sambuc       << Args->getArgString(MissingArgIndex) << MissingArgCount;
1844f4a2713aSLionel Sambuc     Success = false;
1845f4a2713aSLionel Sambuc   }
1846f4a2713aSLionel Sambuc 
1847f4a2713aSLionel Sambuc   // Issue errors on unknown arguments.
1848f4a2713aSLionel Sambuc   for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
1849f4a2713aSLionel Sambuc          ie = Args->filtered_end(); it != ie; ++it) {
1850f4a2713aSLionel Sambuc     Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args);
1851f4a2713aSLionel Sambuc     Success = false;
1852f4a2713aSLionel Sambuc   }
1853f4a2713aSLionel Sambuc 
1854f4a2713aSLionel Sambuc   Success = ParseAnalyzerArgs(*Res.getAnalyzerOpts(), *Args, Diags) && Success;
1855f4a2713aSLionel Sambuc   Success = ParseMigratorArgs(Res.getMigratorOpts(), *Args) && Success;
1856f4a2713aSLionel Sambuc   ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), *Args);
1857f4a2713aSLionel Sambuc   Success = ParseDiagnosticArgs(Res.getDiagnosticOpts(), *Args, &Diags)
1858f4a2713aSLionel Sambuc             && Success;
1859f4a2713aSLionel Sambuc   ParseCommentArgs(Res.getLangOpts()->CommentOpts, *Args);
1860f4a2713aSLionel Sambuc   ParseFileSystemArgs(Res.getFileSystemOpts(), *Args);
1861f4a2713aSLionel Sambuc   // FIXME: We shouldn't have to pass the DashX option around here
1862f4a2713aSLionel Sambuc   InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags);
1863*0a6a1f1dSLionel Sambuc   ParseTargetArgs(Res.getTargetOpts(), *Args);
1864*0a6a1f1dSLionel Sambuc   Success = ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, DashX, Diags,
1865*0a6a1f1dSLionel Sambuc                              Res.getTargetOpts()) && Success;
1866f4a2713aSLionel Sambuc   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args);
1867f4a2713aSLionel Sambuc   if (DashX != IK_AST && DashX != IK_LLVM_IR) {
1868f4a2713aSLionel Sambuc     ParseLangArgs(*Res.getLangOpts(), *Args, DashX, Diags);
1869f4a2713aSLionel Sambuc     if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC)
1870f4a2713aSLionel Sambuc       Res.getLangOpts()->ObjCExceptions = 1;
1871f4a2713aSLionel Sambuc   }
1872f4a2713aSLionel Sambuc   // FIXME: ParsePreprocessorArgs uses the FileManager to read the contents of
1873f4a2713aSLionel Sambuc   // PCH file and find the original header name. Remove the need to do that in
1874f4a2713aSLionel Sambuc   // ParsePreprocessorArgs and remove the FileManager
1875f4a2713aSLionel Sambuc   // parameters from the function and the "FileManager.h" #include.
1876f4a2713aSLionel Sambuc   FileManager FileMgr(Res.getFileSystemOpts());
1877f4a2713aSLionel Sambuc   ParsePreprocessorArgs(Res.getPreprocessorOpts(), *Args, FileMgr, Diags);
1878f4a2713aSLionel Sambuc   ParsePreprocessorOutputArgs(Res.getPreprocessorOutputOpts(), *Args,
1879f4a2713aSLionel Sambuc                               Res.getFrontendOpts().ProgramAction);
1880f4a2713aSLionel Sambuc   return Success;
1881f4a2713aSLionel Sambuc }
1882f4a2713aSLionel Sambuc 
1883f4a2713aSLionel Sambuc namespace {
1884f4a2713aSLionel Sambuc 
1885f4a2713aSLionel Sambuc   class ModuleSignature {
1886f4a2713aSLionel Sambuc     SmallVector<uint64_t, 16> Data;
1887f4a2713aSLionel Sambuc     unsigned CurBit;
1888f4a2713aSLionel Sambuc     uint64_t CurValue;
1889f4a2713aSLionel Sambuc 
1890f4a2713aSLionel Sambuc   public:
ModuleSignature()1891f4a2713aSLionel Sambuc     ModuleSignature() : CurBit(0), CurValue(0) { }
1892f4a2713aSLionel Sambuc 
1893f4a2713aSLionel Sambuc     void add(uint64_t Value, unsigned Bits);
1894f4a2713aSLionel Sambuc     void add(StringRef Value);
1895f4a2713aSLionel Sambuc     void flush();
1896f4a2713aSLionel Sambuc 
1897f4a2713aSLionel Sambuc     llvm::APInt getAsInteger() const;
1898f4a2713aSLionel Sambuc   };
1899f4a2713aSLionel Sambuc }
1900f4a2713aSLionel Sambuc 
add(uint64_t Value,unsigned int NumBits)1901f4a2713aSLionel Sambuc void ModuleSignature::add(uint64_t Value, unsigned int NumBits) {
1902f4a2713aSLionel Sambuc   CurValue |= Value << CurBit;
1903f4a2713aSLionel Sambuc   if (CurBit + NumBits < 64) {
1904f4a2713aSLionel Sambuc     CurBit += NumBits;
1905f4a2713aSLionel Sambuc     return;
1906f4a2713aSLionel Sambuc   }
1907f4a2713aSLionel Sambuc 
1908f4a2713aSLionel Sambuc   // Add the current word.
1909f4a2713aSLionel Sambuc   Data.push_back(CurValue);
1910f4a2713aSLionel Sambuc 
1911f4a2713aSLionel Sambuc   if (CurBit)
1912f4a2713aSLionel Sambuc     CurValue = Value >> (64-CurBit);
1913f4a2713aSLionel Sambuc   else
1914f4a2713aSLionel Sambuc     CurValue = 0;
1915f4a2713aSLionel Sambuc   CurBit = (CurBit+NumBits) & 63;
1916f4a2713aSLionel Sambuc }
1917f4a2713aSLionel Sambuc 
flush()1918f4a2713aSLionel Sambuc void ModuleSignature::flush() {
1919f4a2713aSLionel Sambuc   if (CurBit == 0)
1920f4a2713aSLionel Sambuc     return;
1921f4a2713aSLionel Sambuc 
1922f4a2713aSLionel Sambuc   Data.push_back(CurValue);
1923f4a2713aSLionel Sambuc   CurBit = 0;
1924f4a2713aSLionel Sambuc   CurValue = 0;
1925f4a2713aSLionel Sambuc }
1926f4a2713aSLionel Sambuc 
add(StringRef Value)1927f4a2713aSLionel Sambuc void ModuleSignature::add(StringRef Value) {
1928f4a2713aSLionel Sambuc   for (StringRef::iterator I = Value.begin(), IEnd = Value.end(); I != IEnd;++I)
1929f4a2713aSLionel Sambuc     add(*I, 8);
1930f4a2713aSLionel Sambuc }
1931f4a2713aSLionel Sambuc 
getAsInteger() const1932f4a2713aSLionel Sambuc llvm::APInt ModuleSignature::getAsInteger() const {
1933f4a2713aSLionel Sambuc   return llvm::APInt(Data.size() * 64, Data);
1934f4a2713aSLionel Sambuc }
1935f4a2713aSLionel Sambuc 
getModuleHash() const1936f4a2713aSLionel Sambuc std::string CompilerInvocation::getModuleHash() const {
1937f4a2713aSLionel Sambuc   // Note: For QoI reasons, the things we use as a hash here should all be
1938f4a2713aSLionel Sambuc   // dumped via the -module-info flag.
1939f4a2713aSLionel Sambuc   using llvm::hash_code;
1940f4a2713aSLionel Sambuc   using llvm::hash_value;
1941f4a2713aSLionel Sambuc   using llvm::hash_combine;
1942f4a2713aSLionel Sambuc 
1943f4a2713aSLionel Sambuc   // Start the signature with the compiler version.
1944f4a2713aSLionel Sambuc   // FIXME: We'd rather use something more cryptographically sound than
1945f4a2713aSLionel Sambuc   // CityHash, but this will do for now.
1946f4a2713aSLionel Sambuc   hash_code code = hash_value(getClangFullRepositoryVersion());
1947f4a2713aSLionel Sambuc 
1948f4a2713aSLionel Sambuc   // Extend the signature with the language options
1949f4a2713aSLionel Sambuc #define LANGOPT(Name, Bits, Default, Description) \
1950f4a2713aSLionel Sambuc    code = hash_combine(code, LangOpts->Name);
1951f4a2713aSLionel Sambuc #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1952f4a2713aSLionel Sambuc   code = hash_combine(code, static_cast<unsigned>(LangOpts->get##Name()));
1953f4a2713aSLionel Sambuc #define BENIGN_LANGOPT(Name, Bits, Default, Description)
1954f4a2713aSLionel Sambuc #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
1955f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.def"
1956f4a2713aSLionel Sambuc 
1957f4a2713aSLionel Sambuc   // Extend the signature with the target options.
1958f4a2713aSLionel Sambuc   code = hash_combine(code, TargetOpts->Triple, TargetOpts->CPU,
1959*0a6a1f1dSLionel Sambuc                       TargetOpts->ABI);
1960f4a2713aSLionel Sambuc   for (unsigned i = 0, n = TargetOpts->FeaturesAsWritten.size(); i != n; ++i)
1961f4a2713aSLionel Sambuc     code = hash_combine(code, TargetOpts->FeaturesAsWritten[i]);
1962f4a2713aSLionel Sambuc 
1963f4a2713aSLionel Sambuc   // Extend the signature with preprocessor options.
1964f4a2713aSLionel Sambuc   const PreprocessorOptions &ppOpts = getPreprocessorOpts();
1965f4a2713aSLionel Sambuc   const HeaderSearchOptions &hsOpts = getHeaderSearchOpts();
1966f4a2713aSLionel Sambuc   code = hash_combine(code, ppOpts.UsePredefines, ppOpts.DetailedRecord);
1967f4a2713aSLionel Sambuc 
1968f4a2713aSLionel Sambuc   for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
1969f4a2713aSLionel Sambuc             I = getPreprocessorOpts().Macros.begin(),
1970f4a2713aSLionel Sambuc          IEnd = getPreprocessorOpts().Macros.end();
1971f4a2713aSLionel Sambuc        I != IEnd; ++I) {
1972f4a2713aSLionel Sambuc     // If we're supposed to ignore this macro for the purposes of modules,
1973f4a2713aSLionel Sambuc     // don't put it into the hash.
1974f4a2713aSLionel Sambuc     if (!hsOpts.ModulesIgnoreMacros.empty()) {
1975f4a2713aSLionel Sambuc       // Check whether we're ignoring this macro.
1976f4a2713aSLionel Sambuc       StringRef MacroDef = I->first;
1977f4a2713aSLionel Sambuc       if (hsOpts.ModulesIgnoreMacros.count(MacroDef.split('=').first))
1978f4a2713aSLionel Sambuc         continue;
1979f4a2713aSLionel Sambuc     }
1980f4a2713aSLionel Sambuc 
1981f4a2713aSLionel Sambuc     code = hash_combine(code, I->first, I->second);
1982f4a2713aSLionel Sambuc   }
1983f4a2713aSLionel Sambuc 
1984f4a2713aSLionel Sambuc   // Extend the signature with the sysroot.
1985f4a2713aSLionel Sambuc   code = hash_combine(code, hsOpts.Sysroot, hsOpts.UseBuiltinIncludes,
1986f4a2713aSLionel Sambuc                       hsOpts.UseStandardSystemIncludes,
1987f4a2713aSLionel Sambuc                       hsOpts.UseStandardCXXIncludes,
1988f4a2713aSLionel Sambuc                       hsOpts.UseLibcxx);
1989*0a6a1f1dSLionel Sambuc   code = hash_combine(code, hsOpts.ResourceDir);
1990*0a6a1f1dSLionel Sambuc 
1991*0a6a1f1dSLionel Sambuc   // Extend the signature with the user build path.
1992*0a6a1f1dSLionel Sambuc   code = hash_combine(code, hsOpts.ModuleUserBuildPath);
1993f4a2713aSLionel Sambuc 
1994f4a2713aSLionel Sambuc   // Darwin-specific hack: if we have a sysroot, use the contents and
1995f4a2713aSLionel Sambuc   // modification time of
1996f4a2713aSLionel Sambuc   //   $sysroot/System/Library/CoreServices/SystemVersion.plist
1997f4a2713aSLionel Sambuc   // as part of the module hash.
1998f4a2713aSLionel Sambuc   if (!hsOpts.Sysroot.empty()) {
1999f4a2713aSLionel Sambuc     SmallString<128> systemVersionFile;
2000f4a2713aSLionel Sambuc     systemVersionFile += hsOpts.Sysroot;
2001f4a2713aSLionel Sambuc     llvm::sys::path::append(systemVersionFile, "System");
2002f4a2713aSLionel Sambuc     llvm::sys::path::append(systemVersionFile, "Library");
2003f4a2713aSLionel Sambuc     llvm::sys::path::append(systemVersionFile, "CoreServices");
2004f4a2713aSLionel Sambuc     llvm::sys::path::append(systemVersionFile, "SystemVersion.plist");
2005*0a6a1f1dSLionel Sambuc 
2006*0a6a1f1dSLionel Sambuc     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer =
2007*0a6a1f1dSLionel Sambuc         llvm::MemoryBuffer::getFile(systemVersionFile.str());
2008*0a6a1f1dSLionel Sambuc     if (buffer) {
2009f4a2713aSLionel Sambuc       code = hash_combine(code, buffer.get()->getBuffer());
2010f4a2713aSLionel Sambuc 
2011f4a2713aSLionel Sambuc       struct stat statBuf;
2012f4a2713aSLionel Sambuc       if (stat(systemVersionFile.c_str(), &statBuf) == 0)
2013f4a2713aSLionel Sambuc         code = hash_combine(code, statBuf.st_mtime);
2014f4a2713aSLionel Sambuc     }
2015f4a2713aSLionel Sambuc   }
2016f4a2713aSLionel Sambuc 
2017f4a2713aSLionel Sambuc   return llvm::APInt(64, code).toString(36, /*Signed=*/false);
2018f4a2713aSLionel Sambuc }
2019f4a2713aSLionel Sambuc 
2020f4a2713aSLionel Sambuc namespace clang {
2021f4a2713aSLionel Sambuc 
2022*0a6a1f1dSLionel Sambuc template<typename IntTy>
getLastArgIntValueImpl(const ArgList & Args,OptSpecifier Id,IntTy Default,DiagnosticsEngine * Diags)2023*0a6a1f1dSLionel Sambuc static IntTy getLastArgIntValueImpl(const ArgList &Args, OptSpecifier Id,
2024*0a6a1f1dSLionel Sambuc                                     IntTy Default,
2025f4a2713aSLionel Sambuc                                     DiagnosticsEngine *Diags) {
2026*0a6a1f1dSLionel Sambuc   IntTy Res = Default;
2027f4a2713aSLionel Sambuc   if (Arg *A = Args.getLastArg(Id)) {
2028f4a2713aSLionel Sambuc     if (StringRef(A->getValue()).getAsInteger(10, Res)) {
2029f4a2713aSLionel Sambuc       if (Diags)
2030f4a2713aSLionel Sambuc         Diags->Report(diag::err_drv_invalid_int_value) << A->getAsString(Args)
2031f4a2713aSLionel Sambuc                                                        << A->getValue();
2032f4a2713aSLionel Sambuc     }
2033f4a2713aSLionel Sambuc   }
2034f4a2713aSLionel Sambuc   return Res;
2035f4a2713aSLionel Sambuc }
2036*0a6a1f1dSLionel Sambuc 
2037*0a6a1f1dSLionel Sambuc 
2038*0a6a1f1dSLionel Sambuc // Declared in clang/Frontend/Utils.h.
getLastArgIntValue(const ArgList & Args,OptSpecifier Id,int Default,DiagnosticsEngine * Diags)2039*0a6a1f1dSLionel Sambuc int getLastArgIntValue(const ArgList &Args, OptSpecifier Id, int Default,
2040*0a6a1f1dSLionel Sambuc                        DiagnosticsEngine *Diags) {
2041*0a6a1f1dSLionel Sambuc   return getLastArgIntValueImpl<int>(Args, Id, Default, Diags);
2042f4a2713aSLionel Sambuc }
2043*0a6a1f1dSLionel Sambuc 
getLastArgUInt64Value(const ArgList & Args,OptSpecifier Id,uint64_t Default,DiagnosticsEngine * Diags)2044*0a6a1f1dSLionel Sambuc uint64_t getLastArgUInt64Value(const ArgList &Args, OptSpecifier Id,
2045*0a6a1f1dSLionel Sambuc                                uint64_t Default,
2046*0a6a1f1dSLionel Sambuc                                DiagnosticsEngine *Diags) {
2047*0a6a1f1dSLionel Sambuc   return getLastArgIntValueImpl<uint64_t>(Args, Id, Default, Diags);
2048*0a6a1f1dSLionel Sambuc }
2049*0a6a1f1dSLionel Sambuc 
BuryPointer(const void * Ptr)2050*0a6a1f1dSLionel Sambuc void BuryPointer(const void *Ptr) {
2051*0a6a1f1dSLionel Sambuc   // This function may be called only a small fixed amount of times per each
2052*0a6a1f1dSLionel Sambuc   // invocation, otherwise we do actually have a leak which we want to report.
2053*0a6a1f1dSLionel Sambuc   // If this function is called more than kGraveYardMaxSize times, the pointers
2054*0a6a1f1dSLionel Sambuc   // will not be properly buried and a leak detector will report a leak, which
2055*0a6a1f1dSLionel Sambuc   // is what we want in such case.
2056*0a6a1f1dSLionel Sambuc   static const size_t kGraveYardMaxSize = 16;
2057*0a6a1f1dSLionel Sambuc   LLVM_ATTRIBUTE_UNUSED static const void *GraveYard[kGraveYardMaxSize];
2058*0a6a1f1dSLionel Sambuc #if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix)
2059*0a6a1f1dSLionel Sambuc   static std::atomic<unsigned> GraveYardSize;
2060*0a6a1f1dSLionel Sambuc #else
2061*0a6a1f1dSLionel Sambuc   static unsigned GraveYardSize;
2062*0a6a1f1dSLionel Sambuc #endif // !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix)
2063*0a6a1f1dSLionel Sambuc   unsigned Idx = GraveYardSize++;
2064*0a6a1f1dSLionel Sambuc   if (Idx >= kGraveYardMaxSize)
2065*0a6a1f1dSLionel Sambuc     return;
2066*0a6a1f1dSLionel Sambuc   GraveYard[Idx] = Ptr;
2067*0a6a1f1dSLionel Sambuc }
2068*0a6a1f1dSLionel Sambuc 
2069*0a6a1f1dSLionel Sambuc IntrusiveRefCntPtr<vfs::FileSystem>
createVFSFromCompilerInvocation(const CompilerInvocation & CI,DiagnosticsEngine & Diags)2070*0a6a1f1dSLionel Sambuc createVFSFromCompilerInvocation(const CompilerInvocation &CI,
2071*0a6a1f1dSLionel Sambuc                                 DiagnosticsEngine &Diags) {
2072*0a6a1f1dSLionel Sambuc   if (CI.getHeaderSearchOpts().VFSOverlayFiles.empty())
2073*0a6a1f1dSLionel Sambuc     return vfs::getRealFileSystem();
2074*0a6a1f1dSLionel Sambuc 
2075*0a6a1f1dSLionel Sambuc   IntrusiveRefCntPtr<vfs::OverlayFileSystem>
2076*0a6a1f1dSLionel Sambuc     Overlay(new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
2077*0a6a1f1dSLionel Sambuc   // earlier vfs files are on the bottom
2078*0a6a1f1dSLionel Sambuc   for (const std::string &File : CI.getHeaderSearchOpts().VFSOverlayFiles) {
2079*0a6a1f1dSLionel Sambuc     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
2080*0a6a1f1dSLionel Sambuc         llvm::MemoryBuffer::getFile(File);
2081*0a6a1f1dSLionel Sambuc     if (!Buffer) {
2082*0a6a1f1dSLionel Sambuc       Diags.Report(diag::err_missing_vfs_overlay_file) << File;
2083*0a6a1f1dSLionel Sambuc       return IntrusiveRefCntPtr<vfs::FileSystem>();
2084*0a6a1f1dSLionel Sambuc     }
2085*0a6a1f1dSLionel Sambuc 
2086*0a6a1f1dSLionel Sambuc     IntrusiveRefCntPtr<vfs::FileSystem> FS =
2087*0a6a1f1dSLionel Sambuc         vfs::getVFSFromYAML(std::move(Buffer.get()), /*DiagHandler*/ nullptr);
2088*0a6a1f1dSLionel Sambuc     if (!FS.get()) {
2089*0a6a1f1dSLionel Sambuc       Diags.Report(diag::err_invalid_vfs_overlay) << File;
2090*0a6a1f1dSLionel Sambuc       return IntrusiveRefCntPtr<vfs::FileSystem>();
2091*0a6a1f1dSLionel Sambuc     }
2092*0a6a1f1dSLionel Sambuc     Overlay->pushOverlay(FS);
2093*0a6a1f1dSLionel Sambuc   }
2094*0a6a1f1dSLionel Sambuc   return Overlay;
2095*0a6a1f1dSLionel Sambuc }
2096*0a6a1f1dSLionel Sambuc } // end namespace clang
2097