blob: 4f01c301383c96159787a182734173b57d2aa12d [file] [log] [blame]
Daniel Dunbar0498cfc2009-11-10 19:51:53 +00001//===--- Options.cpp - clang-cc Option Handling ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// This file contains "pure" option handling, it is only responsible for turning
11// the options into internal *Option classes, but shouldn't have any other
12// logic.
13
14#include "Options.h"
Daniel Dunbar339c1342009-11-11 08:13:55 +000015#include "clang/Basic/LangOptions.h"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Frontend/AnalysisConsumer.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000018#include "clang/CodeGen/CodeGenOptions.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000019#include "clang/Frontend/DependencyOutputOptions.h"
Daniel Dunbar0db4b762009-11-11 08:13:40 +000020#include "clang/Frontend/DiagnosticOptions.h"
Daniel Dunbar26266882009-11-12 23:52:32 +000021#include "clang/Frontend/FrontendOptions.h"
Daniel Dunbarf7973292009-11-11 08:13:32 +000022#include "clang/Frontend/HeaderSearchOptions.h"
Daniel Dunbarb52d2432009-11-11 06:10:03 +000023#include "clang/Frontend/PCHReader.h"
24#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbar29cf7462009-11-11 10:07:44 +000025#include "clang/Frontend/PreprocessorOutputOptions.h"
Daniel Dunbarb52d2432009-11-11 06:10:03 +000026#include "llvm/ADT/STLExtras.h"
Daniel Dunbar0498cfc2009-11-10 19:51:53 +000027#include "llvm/ADT/StringMap.h"
28#include "llvm/Support/CommandLine.h"
Dan Gohman0063e982009-11-10 21:21:27 +000029#include <stdio.h>
Daniel Dunbar0498cfc2009-11-10 19:51:53 +000030
31using namespace clang;
32
33//===----------------------------------------------------------------------===//
Daniel Dunbar339c1342009-11-11 08:13:55 +000034// Analyzer Options
35//===----------------------------------------------------------------------===//
36
37namespace analyzeroptions {
38
39static llvm::cl::list<Analyses>
40AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"),
41llvm::cl::values(
42#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
43clEnumValN(NAME, CMDFLAG, DESC),
44#include "clang/Frontend/Analyses.def"
45clEnumValEnd));
46
47static llvm::cl::opt<AnalysisStores>
48AnalysisStoreOpt("analyzer-store",
49 llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"),
50 llvm::cl::init(BasicStoreModel),
51 llvm::cl::values(
52#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\
53clEnumValN(NAME##Model, CMDFLAG, DESC),
54#include "clang/Frontend/Analyses.def"
55clEnumValEnd));
56
57static llvm::cl::opt<AnalysisConstraints>
58AnalysisConstraintsOpt("analyzer-constraints",
59 llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"),
60 llvm::cl::init(RangeConstraintsModel),
61 llvm::cl::values(
62#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\
63clEnumValN(NAME##Model, CMDFLAG, DESC),
64#include "clang/Frontend/Analyses.def"
65clEnumValEnd));
66
67static llvm::cl::opt<AnalysisDiagClients>
68AnalysisDiagOpt("analyzer-output",
69 llvm::cl::desc("Source Code Analysis - Output Options"),
70 llvm::cl::init(PD_HTML),
71 llvm::cl::values(
72#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\
73clEnumValN(PD_##NAME, CMDFLAG, DESC),
74#include "clang/Frontend/Analyses.def"
75clEnumValEnd));
76
77static llvm::cl::opt<bool>
78AnalyzeAll("analyzer-opt-analyze-headers",
79 llvm::cl::desc("Force the static analyzer to analyze "
80 "functions defined in header files"));
Ted Kremenekeb941132009-11-13 01:15:47 +000081
Daniel Dunbar339c1342009-11-11 08:13:55 +000082static llvm::cl::opt<bool>
83AnalyzerDisplayProgress("analyzer-display-progress",
Ted Kremenekeb941132009-11-13 01:15:47 +000084 llvm::cl::desc("Emit verbose output about the analyzer's progress"));
Daniel Dunbar339c1342009-11-11 08:13:55 +000085
Ted Kremenekeb941132009-11-13 01:15:47 +000086static llvm::cl::opt<bool>
87AnalyzerExperimentalChecks("analyzer-experimental-checks",
88 llvm::cl::desc("Use experimental path-sensitive checks"));
Ted Kremenek8382cf52009-11-13 18:46:29 +000089
90static llvm::cl::opt<bool>
91AnalyzerExperimentalInternalChecks("analyzer-experimental-internal-checks",
92 llvm::cl::desc("Use new default path-sensitive checks currently in testing"));
Ted Kremenekeb941132009-11-13 01:15:47 +000093
Daniel Dunbar339c1342009-11-11 08:13:55 +000094static llvm::cl::opt<std::string>
95AnalyzeSpecificFunction("analyze-function",
96 llvm::cl::desc("Run analysis on specific function"));
97
98static llvm::cl::opt<bool>
99EagerlyAssume("analyzer-eagerly-assume",
100 llvm::cl::init(false),
101 llvm::cl::desc("Eagerly assume the truth/falseness of some "
Ted Kremenekeb941132009-11-13 01:15:47 +0000102 "symbolic constraints"));
Daniel Dunbar339c1342009-11-11 08:13:55 +0000103
104static llvm::cl::opt<bool>
105PurgeDead("analyzer-purge-dead",
106 llvm::cl::init(true),
107 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
Ted Kremenekeb941132009-11-13 01:15:47 +0000108 " processing a statement"));
Daniel Dunbar339c1342009-11-11 08:13:55 +0000109
110static llvm::cl::opt<bool>
111TrimGraph("trim-egraph",
112 llvm::cl::desc("Only show error-related paths in the analysis graph"));
113
114static llvm::cl::opt<bool>
115VisualizeEGDot("analyzer-viz-egraph-graphviz",
116 llvm::cl::desc("Display exploded graph using GraphViz"));
117
118static llvm::cl::opt<bool>
119VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
120 llvm::cl::desc("Display exploded graph using Ubigraph"));
121
122}
123
124void clang::InitializeAnalyzerOptions(AnalyzerOptions &Opts) {
125 using namespace analyzeroptions;
126 Opts.AnalysisList = AnalysisList;
127 Opts.AnalysisStoreOpt = AnalysisStoreOpt;
128 Opts.AnalysisConstraintsOpt = AnalysisConstraintsOpt;
129 Opts.AnalysisDiagOpt = AnalysisDiagOpt;
130 Opts.VisualizeEGDot = VisualizeEGDot;
131 Opts.VisualizeEGUbi = VisualizeEGUbi;
132 Opts.AnalyzeAll = AnalyzeAll;
133 Opts.AnalyzerDisplayProgress = AnalyzerDisplayProgress;
134 Opts.PurgeDead = PurgeDead;
135 Opts.EagerlyAssume = EagerlyAssume;
136 Opts.AnalyzeSpecificFunction = AnalyzeSpecificFunction;
Ted Kremenekeb941132009-11-13 01:15:47 +0000137 Opts.EnableExperimentalChecks = AnalyzerExperimentalChecks;
Ted Kremenek8382cf52009-11-13 18:46:29 +0000138 Opts.EnableExperimentalInternalChecks = AnalyzerExperimentalInternalChecks;
Daniel Dunbar339c1342009-11-11 08:13:55 +0000139 Opts.TrimGraph = TrimGraph;
140}
141
142
143//===----------------------------------------------------------------------===//
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000144// Code Generation Options
145//===----------------------------------------------------------------------===//
146
147namespace codegenoptions {
148
149static llvm::cl::opt<bool>
150DisableLLVMOptimizations("disable-llvm-optzns",
151 llvm::cl::desc("Don't run LLVM optimization passes"));
152
153static llvm::cl::opt<bool>
154DisableRedZone("disable-red-zone",
155 llvm::cl::desc("Do not emit code that uses the red zone."),
156 llvm::cl::init(false));
157
158static llvm::cl::opt<bool>
159GenerateDebugInfo("g",
160 llvm::cl::desc("Generate source level debug information"));
161
162static llvm::cl::opt<bool>
163NoCommon("fno-common",
164 llvm::cl::desc("Compile common globals like normal definitions"),
165 llvm::cl::ValueDisallowed);
166
167static llvm::cl::opt<bool>
168NoImplicitFloat("no-implicit-float",
169 llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"),
170 llvm::cl::init(false));
171
172static llvm::cl::opt<bool>
173NoMergeConstants("fno-merge-all-constants",
174 llvm::cl::desc("Disallow merging of constants."));
175
176// It might be nice to add bounds to the CommandLine library directly.
177struct OptLevelParser : public llvm::cl::parser<unsigned> {
178 bool parse(llvm::cl::Option &O, llvm::StringRef ArgName,
179 llvm::StringRef Arg, unsigned &Val) {
180 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
181 return true;
182 if (Val > 3)
183 return O.error("'" + Arg + "' invalid optimization level!");
184 return false;
185 }
186};
187static llvm::cl::opt<unsigned, false, OptLevelParser>
188OptLevel("O", llvm::cl::Prefix,
189 llvm::cl::desc("Optimization level"),
190 llvm::cl::init(0));
191
192static llvm::cl::opt<bool>
193OptSize("Os", llvm::cl::desc("Optimize for size"));
194
195static llvm::cl::opt<std::string>
196TargetCPU("mcpu",
197 llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
198
199static llvm::cl::list<std::string>
200TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes"));
201
202}
203
204//===----------------------------------------------------------------------===//
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000205// Dependency Output Options
206//===----------------------------------------------------------------------===//
207
208namespace dependencyoutputoptions {
209
210static llvm::cl::opt<std::string>
211DependencyFile("dependency-file",
212 llvm::cl::desc("Filename (or -) to write dependency output to"));
213
214static llvm::cl::opt<bool>
215DependenciesIncludeSystemHeaders("sys-header-deps",
216 llvm::cl::desc("Include system headers in dependency output"));
217
218static llvm::cl::list<std::string>
219DependencyTargets("MT",
220 llvm::cl::desc("Specify target for dependency"));
221
222static llvm::cl::opt<bool>
223PhonyDependencyTarget("MP",
224 llvm::cl::desc("Create phony target for each dependency "
225 "(other than main file)"));
226
227}
228
229//===----------------------------------------------------------------------===//
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000230// Diagnostic Options
231//===----------------------------------------------------------------------===//
232
233namespace diagnosticoptions {
234
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000235static llvm::cl::opt<std::string>
236DumpBuildInformation("dump-build-information",
237 llvm::cl::value_desc("filename"),
238 llvm::cl::desc("output a dump of some build information to a file"));
239
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000240static llvm::cl::opt<bool>
241NoShowColumn("fno-show-column",
242 llvm::cl::desc("Do not include column number on diagnostics"));
243
244static llvm::cl::opt<bool>
245NoShowLocation("fno-show-source-location",
246 llvm::cl::desc("Do not include source location information with"
247 " diagnostics"));
248
249static llvm::cl::opt<bool>
250NoCaretDiagnostics("fno-caret-diagnostics",
251 llvm::cl::desc("Do not include source line and caret with"
252 " diagnostics"));
253
254static llvm::cl::opt<bool>
255NoDiagnosticsFixIt("fno-diagnostics-fixit-info",
256 llvm::cl::desc("Do not include fixit information in"
257 " diagnostics"));
258
Daniel Dunbar69079432009-11-12 07:28:44 +0000259static llvm::cl::opt<bool> OptNoWarnings("w");
260
261static llvm::cl::opt<bool> OptPedantic("pedantic");
262
263static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
264
265// This gets all -W options, including -Werror, -W[no-]system-headers, etc. The
266// driver has stripped off -Wa,foo etc. The driver has also translated -W to
267// -Wextra, so we don't need to worry about it.
268static llvm::cl::list<std::string>
269OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional);
270
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000271static llvm::cl::opt<bool>
272PrintSourceRangeInfo("fdiagnostics-print-source-range-info",
273 llvm::cl::desc("Print source range spans in numeric form"));
274
275static llvm::cl::opt<bool>
276PrintDiagnosticOption("fdiagnostics-show-option",
277 llvm::cl::desc("Print diagnostic name with mappable diagnostics"));
278
279static llvm::cl::opt<unsigned>
280MessageLength("fmessage-length",
281 llvm::cl::desc("Format message diagnostics so that they fit "
282 "within N columns or fewer, when possible."),
283 llvm::cl::value_desc("N"));
284
285static llvm::cl::opt<bool>
286PrintColorDiagnostic("fcolor-diagnostics",
287 llvm::cl::desc("Use colors in diagnostics"));
288
Daniel Dunbar69079432009-11-12 07:28:44 +0000289static llvm::cl::opt<bool>
290SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false),
291 llvm::cl::desc("Silence ObjC rewriting warnings"));
292
Daniel Dunbar26266882009-11-12 23:52:32 +0000293static llvm::cl::opt<bool>
294VerifyDiagnostics("verify",
295 llvm::cl::desc("Verify emitted diagnostics and warnings"));
296
297}
298
299
300//===----------------------------------------------------------------------===//
301// Frontend Options
302//===----------------------------------------------------------------------===//
303
304namespace frontendoptions {
305
Daniel Dunbar914474c2009-11-13 01:02:10 +0000306static llvm::cl::opt<ParsedSourceLocation>
307CodeCompletionAt("code-completion-at",
308 llvm::cl::value_desc("file:line:column"),
309 llvm::cl::desc("Dump code-completion information at a location"));
310
311static llvm::cl::opt<bool>
312CodeCompletionDebugPrinter("code-completion-debug-printer",
313 llvm::cl::desc("Use the \"debug\" code-completion print"),
314 llvm::cl::init(true));
315
316static llvm::cl::opt<bool>
317CodeCompletionWantsMacros("code-completion-macros",
318 llvm::cl::desc("Include macros in code-completion results"));
319
Daniel Dunbar26266882009-11-12 23:52:32 +0000320static llvm::cl::opt<bool>
321DisableFree("disable-free",
322 llvm::cl::desc("Disable freeing of memory on exit"),
323 llvm::cl::init(false));
324
325static llvm::cl::opt<bool>
326EmptyInputOnly("empty-input-only",
327 llvm::cl::desc("Force running on an empty input file"));
328
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000329static llvm::cl::opt<FrontendOptions::InputKind>
330InputType("x", llvm::cl::desc("Input language type"),
331 llvm::cl::init(FrontendOptions::IK_None),
332 llvm::cl::values(clEnumValN(FrontendOptions::IK_C, "c", "C"),
333 clEnumValN(FrontendOptions::IK_OpenCL, "cl", "OpenCL C"),
334 clEnumValN(FrontendOptions::IK_CXX, "c++", "C++"),
335 clEnumValN(FrontendOptions::IK_ObjC, "objective-c",
336 "Objective C"),
337 clEnumValN(FrontendOptions::IK_ObjCXX, "objective-c++",
338 "Objective C++"),
339 clEnumValN(FrontendOptions::IK_PreprocessedC,
340 "cpp-output",
341 "Preprocessed C"),
342 clEnumValN(FrontendOptions::IK_Asm,
343 "assembler-with-cpp",
344 "Assembly Source Codde"),
345 clEnumValN(FrontendOptions::IK_PreprocessedCXX,
346 "c++-cpp-output",
347 "Preprocessed C++"),
348 clEnumValN(FrontendOptions::IK_PreprocessedObjC,
349 "objective-c-cpp-output",
350 "Preprocessed Objective C"),
351 clEnumValN(FrontendOptions::IK_PreprocessedObjCXX,
352 "objective-c++-cpp-output",
353 "Preprocessed Objective C++"),
354 clEnumValN(FrontendOptions::IK_C, "c-header",
355 "C header"),
356 clEnumValN(FrontendOptions::IK_ObjC, "objective-c-header",
357 "Objective-C header"),
358 clEnumValN(FrontendOptions::IK_CXX, "c++-header",
359 "C++ header"),
360 clEnumValN(FrontendOptions::IK_ObjCXX,
361 "objective-c++-header",
362 "Objective-C++ header"),
363 clEnumValN(FrontendOptions::IK_AST, "ast",
364 "Clang AST"),
365 clEnumValEnd));
366
Daniel Dunbar26266882009-11-12 23:52:32 +0000367static llvm::cl::list<std::string>
368InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
369
370static llvm::cl::opt<std::string>
371InheritanceViewCls("cxx-inheritance-view",
372 llvm::cl::value_desc("class name"),
373 llvm::cl::desc("View C++ inheritance for a specified class"));
374
375static llvm::cl::opt<bool>
376FixItAll("fixit", llvm::cl::desc("Apply fix-it advice to the input source"));
377
Daniel Dunbarc86804b2009-11-12 23:52:56 +0000378static llvm::cl::list<ParsedSourceLocation>
379FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"),
380 llvm::cl::desc("Perform Fix-It modifications at the given source location"));
381
Daniel Dunbar26266882009-11-12 23:52:32 +0000382static llvm::cl::opt<std::string>
383OutputFile("o",
384 llvm::cl::value_desc("path"),
385 llvm::cl::desc("Specify output file"));
386
387static llvm::cl::opt<bool>
388RelocatablePCH("relocatable-pch",
389 llvm::cl::desc("Whether to build a relocatable precompiled "
390 "header"));
391static llvm::cl::opt<bool>
392Stats("print-stats",
393 llvm::cl::desc("Print performance metrics and statistics"));
394
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000395static llvm::cl::opt<std::string>
396TargetABI("target-abi",
397 llvm::cl::desc("Target a particular ABI type"));
398
399static llvm::cl::opt<std::string>
400TargetTriple("triple",
401 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
402
Daniel Dunbar26266882009-11-12 23:52:32 +0000403static llvm::cl::opt<bool>
404TimeReport("ftime-report",
405 llvm::cl::desc("Print the amount of time each "
406 "phase of compilation takes"));
407
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000408}
409
410//===----------------------------------------------------------------------===//
Daniel Dunbar56749082009-11-11 07:26:12 +0000411// Language Options
412//===----------------------------------------------------------------------===//
413
414namespace langoptions {
415
416static llvm::cl::opt<bool>
417AllowBuiltins("fbuiltin", llvm::cl::init(true),
418 llvm::cl::desc("Disable implicit builtin knowledge of functions"));
419
420static llvm::cl::opt<bool>
421AltiVec("faltivec", llvm::cl::desc("Enable AltiVec vector initializer syntax"),
422 llvm::cl::init(false));
423
424static llvm::cl::opt<bool>
425AccessControl("faccess-control",
426 llvm::cl::desc("Enable C++ access control"));
427
428static llvm::cl::opt<bool>
429CharIsSigned("fsigned-char",
430 llvm::cl::desc("Force char to be a signed/unsigned type"));
431
432static llvm::cl::opt<bool>
433DollarsInIdents("fdollars-in-identifiers",
434 llvm::cl::desc("Allow '$' in identifiers"));
435
436static llvm::cl::opt<bool>
437EmitAllDecls("femit-all-decls",
438 llvm::cl::desc("Emit all declarations, even if unused"));
439
440static llvm::cl::opt<bool>
441EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"));
442
443static llvm::cl::opt<bool>
444EnableHeinousExtensions("fheinous-gnu-extensions",
445 llvm::cl::desc("enable GNU extensions that you really really shouldn't use"),
446 llvm::cl::ValueDisallowed, llvm::cl::Hidden);
447
448static llvm::cl::opt<bool>
449Exceptions("fexceptions",
450 llvm::cl::desc("Enable support for exception handling"));
451
452static llvm::cl::opt<bool>
453Freestanding("ffreestanding",
454 llvm::cl::desc("Assert that the compilation takes place in a "
455 "freestanding environment"));
456
457static llvm::cl::opt<bool>
458GNURuntime("fgnu-runtime",
459 llvm::cl::desc("Generate output compatible with the standard GNU "
460 "Objective-C runtime"));
461
462/// LangStds - Language standards we support.
463enum LangStds {
464 lang_unspecified,
465 lang_c89, lang_c94, lang_c99,
466 lang_gnu89, lang_gnu99,
467 lang_cxx98, lang_gnucxx98,
468 lang_cxx0x, lang_gnucxx0x
469};
470static llvm::cl::opt<LangStds>
471LangStd("std", llvm::cl::desc("Language standard to compile for"),
472 llvm::cl::init(lang_unspecified),
473 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
474 clEnumValN(lang_c89, "c90", "ISO C 1990"),
475 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
476 clEnumValN(lang_c94, "iso9899:199409",
477 "ISO C 1990 with amendment 1"),
478 clEnumValN(lang_c99, "c99", "ISO C 1999"),
479 clEnumValN(lang_c99, "c9x", "ISO C 1999"),
480 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
481 clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
482 clEnumValN(lang_gnu89, "gnu89",
483 "ISO C 1990 with GNU extensions"),
484 clEnumValN(lang_gnu99, "gnu99",
485 "ISO C 1999 with GNU extensions (default for C)"),
486 clEnumValN(lang_gnu99, "gnu9x",
487 "ISO C 1999 with GNU extensions"),
488 clEnumValN(lang_cxx98, "c++98",
489 "ISO C++ 1998 with amendments"),
490 clEnumValN(lang_gnucxx98, "gnu++98",
491 "ISO C++ 1998 with amendments and GNU "
492 "extensions (default for C++)"),
493 clEnumValN(lang_cxx0x, "c++0x",
494 "Upcoming ISO C++ 200x with amendments"),
495 clEnumValN(lang_gnucxx0x, "gnu++0x",
496 "Upcoming ISO C++ 200x with amendments and GNU "
497 "extensions"),
498 clEnumValEnd));
499
500static llvm::cl::opt<bool>
501MSExtensions("fms-extensions",
502 llvm::cl::desc("Accept some non-standard constructs used in "
503 "Microsoft header files "));
504
505static llvm::cl::opt<std::string>
506MainFileName("main-file-name",
507 llvm::cl::desc("Main file name to use for debug info"));
508
509static llvm::cl::opt<bool>
510MathErrno("fmath-errno", llvm::cl::init(true),
511 llvm::cl::desc("Require math functions to respect errno"));
512
513static llvm::cl::opt<bool>
514NeXTRuntime("fnext-runtime",
515 llvm::cl::desc("Generate output compatible with the NeXT "
516 "runtime"));
517
518static llvm::cl::opt<bool>
519NoElideConstructors("fno-elide-constructors",
520 llvm::cl::desc("Disable C++ copy constructor elision"));
521
522static llvm::cl::opt<bool>
523NoLaxVectorConversions("fno-lax-vector-conversions",
524 llvm::cl::desc("Disallow implicit conversions between "
525 "vectors with a different number of "
526 "elements or different element types"));
527
528
529static llvm::cl::opt<bool>
530NoOperatorNames("fno-operator-names",
531 llvm::cl::desc("Do not treat C++ operator name keywords as "
532 "synonyms for operators"));
533
534static llvm::cl::opt<std::string>
535ObjCConstantStringClass("fconstant-string-class",
536 llvm::cl::value_desc("class name"),
537 llvm::cl::desc("Specify the class to use for constant "
538 "Objective-C string objects."));
539
540static llvm::cl::opt<bool>
541ObjCEnableGC("fobjc-gc",
542 llvm::cl::desc("Enable Objective-C garbage collection"));
543
544static llvm::cl::opt<bool>
545ObjCExclusiveGC("fobjc-gc-only",
546 llvm::cl::desc("Use GC exclusively for Objective-C related "
547 "memory management"));
548
549static llvm::cl::opt<bool>
550ObjCEnableGCBitmapPrint("print-ivar-layout",
551 llvm::cl::desc("Enable Objective-C Ivar layout bitmap print trace"));
552
553static llvm::cl::opt<bool>
554ObjCNonFragileABI("fobjc-nonfragile-abi",
555 llvm::cl::desc("enable objective-c's nonfragile abi"));
556
557static llvm::cl::opt<bool>
558OverflowChecking("ftrapv",
559 llvm::cl::desc("Trap on integer overflow"),
560 llvm::cl::init(false));
561
562static llvm::cl::opt<unsigned>
563PICLevel("pic-level", llvm::cl::desc("Value for __PIC__"));
564
565static llvm::cl::opt<bool>
566PThread("pthread", llvm::cl::desc("Support POSIX threads in generated code"),
567 llvm::cl::init(false));
568
569static llvm::cl::opt<bool>
570PascalStrings("fpascal-strings",
571 llvm::cl::desc("Recognize and construct Pascal-style "
572 "string literals"));
573
Daniel Dunbar56749082009-11-11 07:26:12 +0000574static llvm::cl::opt<bool>
575Rtti("frtti", llvm::cl::init(true),
576 llvm::cl::desc("Enable generation of rtti information"));
577
578static llvm::cl::opt<bool>
579ShortWChar("fshort-wchar",
580 llvm::cl::desc("Force wchar_t to be a short unsigned int"));
581
582static llvm::cl::opt<bool>
583StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined"));
584
585static llvm::cl::opt<int>
586StackProtector("stack-protector",
587 llvm::cl::desc("Enable stack protectors"),
588 llvm::cl::init(-1));
589
590static llvm::cl::opt<LangOptions::VisibilityMode>
591SymbolVisibility("fvisibility",
592 llvm::cl::desc("Set the default symbol visibility:"),
593 llvm::cl::init(LangOptions::Default),
594 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
595 "Use default symbol visibility"),
596 clEnumValN(LangOptions::Hidden, "hidden",
597 "Use hidden symbol visibility"),
598 clEnumValN(LangOptions::Protected,"protected",
599 "Use protected symbol visibility"),
600 clEnumValEnd));
601
602static llvm::cl::opt<unsigned>
603TemplateDepth("ftemplate-depth", llvm::cl::init(99),
604 llvm::cl::desc("Maximum depth of recursive template "
605 "instantiation"));
606
607static llvm::cl::opt<bool>
608Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences"));
609
610static llvm::cl::opt<bool>
611WritableStrings("fwritable-strings",
612 llvm::cl::desc("Store string literals as writable data"));
613
614}
615
616//===----------------------------------------------------------------------===//
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000617// General Preprocessor Options
618//===----------------------------------------------------------------------===//
619
620namespace preprocessoroptions {
621
622static llvm::cl::list<std::string>
623D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
624 llvm::cl::desc("Predefine the specified macro"));
625
626static llvm::cl::list<std::string>
627ImplicitIncludes("include", llvm::cl::value_desc("file"),
628 llvm::cl::desc("Include file before parsing"));
629static llvm::cl::list<std::string>
630ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"),
631 llvm::cl::desc("Include macros from file before parsing"));
632
633static llvm::cl::opt<std::string>
634ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"),
635 llvm::cl::desc("Include precompiled header file"));
636
637static llvm::cl::opt<std::string>
638ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"),
639 llvm::cl::desc("Include file before parsing"));
640
Daniel Dunbarb3cb98e2009-11-12 02:53:59 +0000641static llvm::cl::opt<std::string>
642TokenCache("token-cache", llvm::cl::value_desc("path"),
643 llvm::cl::desc("Use specified token cache file"));
644
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000645static llvm::cl::list<std::string>
646U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
647 llvm::cl::desc("Undefine the specified macro"));
648
649static llvm::cl::opt<bool>
650UndefMacros("undef", llvm::cl::value_desc("macro"),
651 llvm::cl::desc("undef all system defines"));
652
653}
654
655//===----------------------------------------------------------------------===//
Daniel Dunbarf7973292009-11-11 08:13:32 +0000656// Header Search Options
657//===----------------------------------------------------------------------===//
658
659namespace headersearchoptions {
660
661static llvm::cl::opt<bool>
662nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
663
664static llvm::cl::opt<bool>
665nobuiltininc("nobuiltininc",
666 llvm::cl::desc("Disable builtin #include directories"));
667
668// Various command line options. These four add directories to each chain.
669static llvm::cl::list<std::string>
670F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
671 llvm::cl::desc("Add directory to framework include search path"));
672
673static llvm::cl::list<std::string>
674I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
675 llvm::cl::desc("Add directory to include search path"));
676
677static llvm::cl::list<std::string>
678idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
679 llvm::cl::desc("Add directory to AFTER include search path"));
680
681static llvm::cl::list<std::string>
682iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
683 llvm::cl::desc("Add directory to QUOTE include search path"));
684
685static llvm::cl::list<std::string>
686isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
687 llvm::cl::desc("Add directory to SYSTEM include search path"));
688
689// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
690static llvm::cl::list<std::string>
691iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
692 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
693static llvm::cl::list<std::string>
694iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
695 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
696static llvm::cl::list<std::string>
697iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
698 llvm::cl::Prefix,
699 llvm::cl::desc("Set directory to include search path with prefix"));
700
701static llvm::cl::opt<std::string>
702isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
703 llvm::cl::desc("Set the system root directory (usually /)"));
704
Daniel Dunbar1417c742009-11-12 23:52:46 +0000705static llvm::cl::opt<bool>
706Verbose("v", llvm::cl::desc("Enable verbose output"));
707
Daniel Dunbarf7973292009-11-11 08:13:32 +0000708}
709
710//===----------------------------------------------------------------------===//
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000711// Preprocessed Output Options
712//===----------------------------------------------------------------------===//
713
714namespace preprocessoroutputoptions {
715
716static llvm::cl::opt<bool>
717DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
718
719static llvm::cl::opt<bool>
720EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
721
722static llvm::cl::opt<bool>
723EnableMacroCommentOutput("CC",
724 llvm::cl::desc("Enable comment output in -E mode, "
725 "even from macro expansions"));
726static llvm::cl::opt<bool>
727DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
728 " normal output"));
729static llvm::cl::opt<bool>
730DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
731 "addition to normal output"));
732
733}
734
735//===----------------------------------------------------------------------===//
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000736// Option Object Construction
737//===----------------------------------------------------------------------===//
738
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000739void clang::InitializeCodeGenOptions(CodeGenOptions &Opts,
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000740 const TargetInfo &Target) {
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000741 using namespace codegenoptions;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000742
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000743 // Compute the target features, we need the target to handle this because
744 // features may have dependencies on one another.
745 llvm::StringMap<bool> Features;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000746 Target.getDefaultFeatures(TargetCPU, Features);
747
748 // Apply the user specified deltas.
749 for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
750 ie = TargetFeatures.end(); it != ie; ++it) {
751 const char *Name = it->c_str();
752
753 // FIXME: Don't handle errors like this.
754 if (Name[0] != '-' && Name[0] != '+') {
755 fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
756 Name);
757 exit(1);
758 }
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000759
760 // Apply the feature via the target.
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000761 if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
762 fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
763 Name + 1);
764 exit(1);
765 }
766 }
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000767
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000768 // Add the features to the compile options.
769 //
770 // FIXME: If we are completely confident that we have the right set, we only
771 // need to pass the minuses.
772 for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
773 ie = Features.end(); it != ie; ++it)
774 Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000775
776 // -Os implies -O2
777 Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
778
779 // We must always run at least the always inlining pass.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000780 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
781 : CodeGenOptions::OnlyAlwaysInlining;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000782
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000783 Opts.CPU = TargetCPU;
784 Opts.DebugInfo = GenerateDebugInfo;
785 Opts.DisableLLVMOpts = DisableLLVMOptimizations;
786 Opts.DisableRedZone = DisableRedZone;
787 Opts.MergeAllConstants = !NoMergeConstants;
788 Opts.NoCommon = NoCommon;
789 Opts.NoImplicitFloat = NoImplicitFloat;
790 Opts.OptimizeSize = OptSize;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000791 Opts.SimplifyLibCalls = 1;
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000792 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000793
794#ifdef NDEBUG
795 Opts.VerifyModule = 0;
796#endif
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000797}
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000798
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000799void clang::InitializeDependencyOutputOptions(DependencyOutputOptions &Opts) {
800 using namespace dependencyoutputoptions;
801
802 Opts.OutputFile = DependencyFile;
Daniel Dunbar26266882009-11-12 23:52:32 +0000803 Opts.Targets = DependencyTargets;
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000804 Opts.IncludeSystemHeaders = DependenciesIncludeSystemHeaders;
805 Opts.UsePhonyTargets = PhonyDependencyTarget;
806}
807
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000808void clang::InitializeDiagnosticOptions(DiagnosticOptions &Opts) {
809 using namespace diagnosticoptions;
810
Daniel Dunbar26266882009-11-12 23:52:32 +0000811 Opts.Warnings = OptWarnings;
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000812 Opts.DumpBuildInformation = DumpBuildInformation;
Daniel Dunbar69079432009-11-12 07:28:44 +0000813 Opts.IgnoreWarnings = OptNoWarnings;
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000814 Opts.MessageLength = MessageLength;
Daniel Dunbar69079432009-11-12 07:28:44 +0000815 Opts.NoRewriteMacros = SilenceRewriteMacroWarning;
816 Opts.Pedantic = OptPedantic;
817 Opts.PedanticErrors = OptPedanticErrors;
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000818 Opts.ShowCarets = !NoCaretDiagnostics;
819 Opts.ShowColors = PrintColorDiagnostic;
820 Opts.ShowColumn = !NoShowColumn;
821 Opts.ShowFixits = !NoDiagnosticsFixIt;
822 Opts.ShowLocation = !NoShowLocation;
823 Opts.ShowOptionNames = PrintDiagnosticOption;
824 Opts.ShowSourceRanges = PrintSourceRangeInfo;
Daniel Dunbar26266882009-11-12 23:52:32 +0000825 Opts.VerifyDiagnostics = VerifyDiagnostics;
826}
827
828void clang::InitializeFrontendOptions(FrontendOptions &Opts) {
829 using namespace frontendoptions;
830
Daniel Dunbar914474c2009-11-13 01:02:10 +0000831 Opts.CodeCompletionAt = CodeCompletionAt;
832 Opts.DebugCodeCompletionPrinter = CodeCompletionDebugPrinter;
Daniel Dunbar26266882009-11-12 23:52:32 +0000833 Opts.DisableFree = DisableFree;
834 Opts.EmptyInputOnly = EmptyInputOnly;
835 Opts.FixItAll = FixItAll;
Daniel Dunbarc86804b2009-11-12 23:52:56 +0000836 Opts.FixItLocations = FixItAtLocations;
Daniel Dunbar26266882009-11-12 23:52:32 +0000837 Opts.OutputFile = OutputFile;
Daniel Dunbar914474c2009-11-13 01:02:10 +0000838 Opts.RelocatablePCH = RelocatablePCH;
839 Opts.ShowMacrosInCodeCompletion = CodeCompletionWantsMacros;
840 Opts.ShowStats = Stats;
841 Opts.ShowTimers = TimeReport;
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000842 Opts.TargetABI = TargetABI;
843 Opts.TargetTriple = TargetTriple;
Daniel Dunbar26266882009-11-12 23:52:32 +0000844 Opts.ViewClassInheritance = InheritanceViewCls;
845
846 // '-' is the default input if none is given.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000847 if (InputFilenames.empty()) {
848 FrontendOptions::InputKind IK = InputType;
849 if (IK == FrontendOptions::IK_None) IK = FrontendOptions::IK_C;
850 Opts.Inputs.push_back(std::make_pair(IK, "-"));
851 } else {
852 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
853 FrontendOptions::InputKind IK = InputType;
854 llvm::StringRef Ext =
855 llvm::StringRef(InputFilenames[i]).rsplit('.').second;
856 if (IK == FrontendOptions::IK_None)
857 IK = FrontendOptions::getInputKindForExtension(Ext);
858 Opts.Inputs.push_back(std::make_pair(IK, InputFilenames[i]));
859
860 }
861 }
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000862}
863
Daniel Dunbarf7973292009-11-11 08:13:32 +0000864void clang::InitializeHeaderSearchOptions(HeaderSearchOptions &Opts,
865 llvm::StringRef BuiltinIncludePath,
Daniel Dunbarf7973292009-11-11 08:13:32 +0000866 const LangOptions &Lang) {
867 using namespace headersearchoptions;
868
869 Opts.Sysroot = isysroot;
870 Opts.Verbose = Verbose;
871
872 // Handle -I... and -F... options, walking the lists in parallel.
873 unsigned Iidx = 0, Fidx = 0;
874 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
875 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
876 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
877 ++Iidx;
878 } else {
879 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
880 ++Fidx;
881 }
882 }
883
884 // Consume what's left from whatever list was longer.
885 for (; Iidx != I_dirs.size(); ++Iidx)
886 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
887 for (; Fidx != F_dirs.size(); ++Fidx)
888 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
889
890 // Handle -idirafter... options.
891 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
892 Opts.AddPath(idirafter_dirs[i], frontend::After,
893 false, true, false);
894
895 // Handle -iquote... options.
896 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
897 Opts.AddPath(iquote_dirs[i], frontend::Quoted, false, true, false);
898
899 // Handle -isystem... options.
900 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
901 Opts.AddPath(isystem_dirs[i], frontend::System, false, true, false);
902
903 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
904 // parallel, processing the values in order of occurance to get the right
905 // prefixes.
906 {
907 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
908 unsigned iprefix_idx = 0;
909 unsigned iwithprefix_idx = 0;
910 unsigned iwithprefixbefore_idx = 0;
911 bool iprefix_done = iprefix_vals.empty();
912 bool iwithprefix_done = iwithprefix_vals.empty();
913 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
914 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
915 if (!iprefix_done &&
916 (iwithprefix_done ||
917 iprefix_vals.getPosition(iprefix_idx) <
918 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
919 (iwithprefixbefore_done ||
920 iprefix_vals.getPosition(iprefix_idx) <
921 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
922 Prefix = iprefix_vals[iprefix_idx];
923 ++iprefix_idx;
924 iprefix_done = iprefix_idx == iprefix_vals.size();
925 } else if (!iwithprefix_done &&
926 (iwithprefixbefore_done ||
927 iwithprefix_vals.getPosition(iwithprefix_idx) <
928 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
929 Opts.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
930 frontend::System, false, false, false);
931 ++iwithprefix_idx;
932 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
933 } else {
934 Opts.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
935 frontend::Angled, false, false, false);
936 ++iwithprefixbefore_idx;
937 iwithprefixbefore_done =
938 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
939 }
940 }
941 }
942
943 // Add CPATH environment paths.
944 if (const char *Env = getenv("CPATH"))
945 Opts.EnvIncPath = Env;
946
947 // Add language specific environment paths.
948 if (Lang.CPlusPlus && Lang.ObjC1) {
949 if (const char *Env = getenv("OBJCPLUS_INCLUDE_PATH"))
950 Opts.LangEnvIncPath = Env;
951 } else if (Lang.CPlusPlus) {
952 if (const char *Env = getenv("CPLUS_INCLUDE_PATH"))
953 Opts.LangEnvIncPath = Env;
954 } else if (Lang.ObjC1) {
955 if (const char *Env = getenv("OBJC_INCLUDE_PATH"))
956 Opts.LangEnvIncPath = Env;
957 } else {
958 if (const char *Env = getenv("C_INCLUDE_PATH"))
959 Opts.LangEnvIncPath = Env;
960 }
961
962 if (!nobuiltininc)
963 Opts.BuiltinIncludePath = BuiltinIncludePath;
964
965 Opts.UseStandardIncludes = !nostdinc;
966}
967
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000968void clang::InitializePreprocessorOptions(PreprocessorOptions &Opts) {
969 using namespace preprocessoroptions;
970
971 Opts.setImplicitPCHInclude(ImplicitIncludePCH);
972 Opts.setImplicitPTHInclude(ImplicitIncludePTH);
973
Daniel Dunbarb3cb98e2009-11-12 02:53:59 +0000974 // Select the token cache file, we don't support more than one currently so we
975 // can't have both an implicit-pth and a token cache file.
976 if (TokenCache.getPosition() && ImplicitIncludePTH.getPosition()) {
977 // FIXME: Don't fail like this.
978 fprintf(stderr, "error: cannot use both -token-cache and -include-pth "
979 "options\n");
980 exit(1);
981 }
982 if (TokenCache.getPosition())
983 Opts.setTokenCache(TokenCache);
984 else
985 Opts.setTokenCache(ImplicitIncludePTH);
986
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000987 // Use predefines?
988 Opts.setUsePredefines(!UndefMacros);
989
990 // Add macros from the command line.
991 unsigned d = 0, D = D_macros.size();
992 unsigned u = 0, U = U_macros.size();
993 while (d < D || u < U) {
994 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
995 Opts.addMacroDef(D_macros[d++]);
996 else
997 Opts.addMacroUndef(U_macros[u++]);
998 }
999
1000 // If -imacros are specified, include them now. These are processed before
1001 // any -include directives.
1002 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
1003 Opts.addMacroInclude(ImplicitMacroIncludes[i]);
1004
1005 // Add the ordered list of -includes, sorting in the implicit include options
1006 // at the appropriate location.
1007 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
1008 std::string OriginalFile;
1009
1010 if (!ImplicitIncludePTH.empty())
1011 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
1012 &ImplicitIncludePTH));
1013 if (!ImplicitIncludePCH.empty()) {
1014 OriginalFile = PCHReader::getOriginalSourceFile(ImplicitIncludePCH);
1015 // FIXME: Don't fail like this.
1016 if (OriginalFile.empty())
1017 exit(1);
1018 OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
1019 &OriginalFile));
1020 }
1021 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
1022 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
1023 &ImplicitIncludes[i]));
1024 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
1025
1026 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
1027 Opts.addInclude(*OrderedPaths[i].second);
1028}
Daniel Dunbar56749082009-11-11 07:26:12 +00001029
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001030void clang::InitializeLangOptions(LangOptions &Options,
1031 FrontendOptions::InputKind IK,
Daniel Dunbar56749082009-11-11 07:26:12 +00001032 TargetInfo &Target,
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001033 const CodeGenOptions &CodeGenOpts) {
Daniel Dunbar56749082009-11-11 07:26:12 +00001034 using namespace langoptions;
1035
1036 bool NoPreprocess = false;
1037
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001038 switch (IK) {
1039 case FrontendOptions::IK_None:
1040 case FrontendOptions::IK_AST:
1041 assert(0 && "Invalid input kind!");
1042 case FrontendOptions::IK_Asm:
Daniel Dunbar56749082009-11-11 07:26:12 +00001043 Options.AsmPreprocessor = 1;
1044 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001045 case FrontendOptions::IK_PreprocessedC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001046 NoPreprocess = true;
1047 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001048 case FrontendOptions::IK_C:
Daniel Dunbar56749082009-11-11 07:26:12 +00001049 // Do nothing.
1050 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001051 case FrontendOptions::IK_PreprocessedCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001052 NoPreprocess = true;
1053 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001054 case FrontendOptions::IK_CXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001055 Options.CPlusPlus = 1;
1056 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001057 case FrontendOptions::IK_PreprocessedObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001058 NoPreprocess = true;
1059 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001060 case FrontendOptions::IK_ObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001061 Options.ObjC1 = Options.ObjC2 = 1;
1062 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001063 case FrontendOptions::IK_PreprocessedObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001064 NoPreprocess = true;
1065 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001066 case FrontendOptions::IK_ObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001067 Options.ObjC1 = Options.ObjC2 = 1;
1068 Options.CPlusPlus = 1;
1069 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001070 case FrontendOptions::IK_OpenCL:
Daniel Dunbar56749082009-11-11 07:26:12 +00001071 Options.OpenCL = 1;
1072 Options.AltiVec = 1;
1073 Options.CXXOperatorNames = 1;
1074 Options.LaxVectorConversions = 1;
1075 break;
1076 }
1077
1078 if (ObjCExclusiveGC)
1079 Options.setGCMode(LangOptions::GCOnly);
1080 else if (ObjCEnableGC)
1081 Options.setGCMode(LangOptions::HybridGC);
1082
1083 if (ObjCEnableGCBitmapPrint)
1084 Options.ObjCGCBitmapPrint = 1;
1085
1086 if (AltiVec)
1087 Options.AltiVec = 1;
1088
1089 if (PThread)
1090 Options.POSIXThreads = 1;
1091
1092 Options.setVisibilityMode(SymbolVisibility);
1093 Options.OverflowChecking = OverflowChecking;
1094
1095
1096 // Allow the target to set the default the language options as it sees fit.
1097 Target.getDefaultLangOptions(Options);
1098
1099 // Pass the map of target features to the target for validation and
1100 // processing.
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001101 Target.HandleTargetFeatures(CodeGenOpts.Features);
Daniel Dunbar56749082009-11-11 07:26:12 +00001102
1103 if (LangStd == lang_unspecified) {
1104 // Based on the base language, pick one.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001105 switch (IK) {
1106 case FrontendOptions::IK_None:
1107 case FrontendOptions::IK_AST:
1108 assert(0 && "Invalid input kind!");
1109 case FrontendOptions::IK_OpenCL:
Daniel Dunbar56749082009-11-11 07:26:12 +00001110 LangStd = lang_c99;
1111 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001112 case FrontendOptions::IK_Asm:
1113 case FrontendOptions::IK_C:
1114 case FrontendOptions::IK_PreprocessedC:
1115 case FrontendOptions::IK_ObjC:
1116 case FrontendOptions::IK_PreprocessedObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001117 LangStd = lang_gnu99;
1118 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001119 case FrontendOptions::IK_CXX:
1120 case FrontendOptions::IK_PreprocessedCXX:
1121 case FrontendOptions::IK_ObjCXX:
1122 case FrontendOptions::IK_PreprocessedObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001123 LangStd = lang_gnucxx98;
1124 break;
1125 }
1126 }
1127
1128 switch (LangStd) {
1129 default: assert(0 && "Unknown language standard!");
1130
1131 // Fall through from newer standards to older ones. This isn't really right.
1132 // FIXME: Enable specifically the right features based on the language stds.
1133 case lang_gnucxx0x:
1134 case lang_cxx0x:
1135 Options.CPlusPlus0x = 1;
1136 // FALL THROUGH
1137 case lang_gnucxx98:
1138 case lang_cxx98:
1139 Options.CPlusPlus = 1;
1140 Options.CXXOperatorNames = !NoOperatorNames;
1141 // FALL THROUGH.
1142 case lang_gnu99:
1143 case lang_c99:
1144 Options.C99 = 1;
1145 Options.HexFloats = 1;
1146 // FALL THROUGH.
1147 case lang_gnu89:
1148 Options.BCPLComment = 1; // Only for C99/C++.
1149 // FALL THROUGH.
1150 case lang_c94:
1151 Options.Digraphs = 1; // C94, C99, C++.
1152 // FALL THROUGH.
1153 case lang_c89:
1154 break;
1155 }
1156
1157 // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc.
1158 switch (LangStd) {
1159 default: assert(0 && "Unknown language standard!");
1160 case lang_gnucxx0x:
1161 case lang_gnucxx98:
1162 case lang_gnu99:
1163 case lang_gnu89:
1164 Options.GNUMode = 1;
1165 break;
1166 case lang_cxx0x:
1167 case lang_cxx98:
1168 case lang_c99:
1169 case lang_c94:
1170 case lang_c89:
1171 Options.GNUMode = 0;
1172 break;
1173 }
1174
1175 if (Options.CPlusPlus) {
1176 Options.C99 = 0;
1177 Options.HexFloats = 0;
1178 }
1179
1180 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
1181 Options.ImplicitInt = 1;
1182 else
1183 Options.ImplicitInt = 0;
1184
1185 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
1186 // is specified, or -std is set to a conforming mode.
1187 Options.Trigraphs = !Options.GNUMode;
1188 if (Trigraphs.getPosition())
1189 Options.Trigraphs = Trigraphs; // Command line option wins if specified.
1190
1191 // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off
1192 // even if they are normally on for the target. In GNU modes (e.g.
1193 // -std=gnu99) the default for blocks depends on the target settings.
1194 // However, blocks are not turned off when compiling Obj-C or Obj-C++ code.
1195 if (!Options.ObjC1 && !Options.GNUMode)
1196 Options.Blocks = 0;
1197
1198 // Default to not accepting '$' in identifiers when preprocessing assembler,
1199 // but do accept when preprocessing C. FIXME: these defaults are right for
1200 // darwin, are they right everywhere?
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001201 Options.DollarIdents = IK != FrontendOptions::IK_Asm;
Daniel Dunbar56749082009-11-11 07:26:12 +00001202 if (DollarsInIdents.getPosition()) // Explicit setting overrides default.
1203 Options.DollarIdents = DollarsInIdents;
1204
1205 if (PascalStrings.getPosition())
1206 Options.PascalStrings = PascalStrings;
1207 if (MSExtensions.getPosition())
1208 Options.Microsoft = MSExtensions;
1209 Options.WritableStrings = WritableStrings;
1210 if (NoLaxVectorConversions.getPosition())
1211 Options.LaxVectorConversions = 0;
1212 Options.Exceptions = Exceptions;
1213 Options.Rtti = Rtti;
1214 if (EnableBlocks.getPosition())
1215 Options.Blocks = EnableBlocks;
1216 if (CharIsSigned.getPosition())
1217 Options.CharIsSigned = CharIsSigned;
1218 if (ShortWChar.getPosition())
1219 Options.ShortWChar = ShortWChar;
1220
1221 if (!AllowBuiltins)
1222 Options.NoBuiltin = 1;
1223 if (Freestanding)
1224 Options.Freestanding = Options.NoBuiltin = 1;
1225
1226 if (EnableHeinousExtensions)
1227 Options.HeinousExtensions = 1;
1228
1229 if (AccessControl)
1230 Options.AccessControl = 1;
1231
1232 Options.ElideConstructors = !NoElideConstructors;
1233
1234 // OpenCL and C++ both have bool, true, false keywords.
1235 Options.Bool = Options.OpenCL | Options.CPlusPlus;
1236
1237 Options.MathErrno = MathErrno;
1238
1239 Options.InstantiationDepth = TemplateDepth;
1240
1241 // Override the default runtime if the user requested it.
1242 if (NeXTRuntime)
1243 Options.NeXTRuntime = 1;
1244 else if (GNURuntime)
1245 Options.NeXTRuntime = 0;
1246
1247 if (!ObjCConstantStringClass.empty())
1248 Options.ObjCConstantStringClass = ObjCConstantStringClass.c_str();
1249
1250 if (ObjCNonFragileABI)
1251 Options.ObjCNonFragileABI = 1;
1252
1253 if (EmitAllDecls)
1254 Options.EmitAllDecls = 1;
1255
1256 // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't support.
1257 Options.OptimizeSize = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001258 Options.Optimize = !!CodeGenOpts.OptimizationLevel;
Daniel Dunbar56749082009-11-11 07:26:12 +00001259
1260 assert(PICLevel <= 2 && "Invalid value for -pic-level");
1261 Options.PICLevel = PICLevel;
1262
1263 Options.GNUInline = !Options.C99;
1264 // FIXME: This is affected by other options (-fno-inline).
1265
1266 // This is the __NO_INLINE__ define, which just depends on things like the
1267 // optimization level and -fno-inline, not actually whether the backend has
1268 // inlining enabled.
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001269 Options.NoInline = !CodeGenOpts.OptimizationLevel;
Daniel Dunbar56749082009-11-11 07:26:12 +00001270
1271 Options.Static = StaticDefine;
1272
1273 switch (StackProtector) {
1274 default:
1275 assert(StackProtector <= 2 && "Invalid value for -stack-protector");
1276 case -1: break;
1277 case 0: Options.setStackProtectorMode(LangOptions::SSPOff); break;
1278 case 1: Options.setStackProtectorMode(LangOptions::SSPOn); break;
1279 case 2: Options.setStackProtectorMode(LangOptions::SSPReq); break;
1280 }
1281
1282 if (MainFileName.getPosition())
1283 Options.setMainFileName(MainFileName.c_str());
1284
1285 Target.setForcedLangOptions(Options);
1286}
Daniel Dunbar29cf7462009-11-11 10:07:44 +00001287
1288void
1289clang::InitializePreprocessorOutputOptions(PreprocessorOutputOptions &Opts) {
1290 using namespace preprocessoroutputoptions;
1291
1292 Opts.ShowCPP = !DumpMacros;
1293 Opts.ShowMacros = DumpMacros || DumpDefines;
1294 Opts.ShowLineMarkers = !DisableLineMarkers;
1295 Opts.ShowComments = EnableCommentOutput;
1296 Opts.ShowMacroComments = EnableMacroCommentOutput;
1297}