blob: 47bed4f89f564a6622725443f90d92be1c3939e3 [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
Daniel Dunbarc86804b2009-11-12 23:52:56 +0000375static llvm::cl::list<ParsedSourceLocation>
376FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"),
377 llvm::cl::desc("Perform Fix-It modifications at the given source location"));
378
Daniel Dunbar26266882009-11-12 23:52:32 +0000379static llvm::cl::opt<std::string>
380OutputFile("o",
381 llvm::cl::value_desc("path"),
382 llvm::cl::desc("Specify output file"));
383
384static llvm::cl::opt<bool>
385RelocatablePCH("relocatable-pch",
386 llvm::cl::desc("Whether to build a relocatable precompiled "
387 "header"));
388static llvm::cl::opt<bool>
389Stats("print-stats",
390 llvm::cl::desc("Print performance metrics and statistics"));
391
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000392static llvm::cl::opt<std::string>
393TargetABI("target-abi",
394 llvm::cl::desc("Target a particular ABI type"));
395
396static llvm::cl::opt<std::string>
397TargetTriple("triple",
398 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
399
Daniel Dunbar26266882009-11-12 23:52:32 +0000400static llvm::cl::opt<bool>
401TimeReport("ftime-report",
402 llvm::cl::desc("Print the amount of time each "
403 "phase of compilation takes"));
404
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000405}
406
407//===----------------------------------------------------------------------===//
Daniel Dunbar56749082009-11-11 07:26:12 +0000408// Language Options
409//===----------------------------------------------------------------------===//
410
411namespace langoptions {
412
413static llvm::cl::opt<bool>
414AllowBuiltins("fbuiltin", llvm::cl::init(true),
415 llvm::cl::desc("Disable implicit builtin knowledge of functions"));
416
417static llvm::cl::opt<bool>
418AltiVec("faltivec", llvm::cl::desc("Enable AltiVec vector initializer syntax"),
419 llvm::cl::init(false));
420
421static llvm::cl::opt<bool>
422AccessControl("faccess-control",
423 llvm::cl::desc("Enable C++ access control"));
424
425static llvm::cl::opt<bool>
426CharIsSigned("fsigned-char",
427 llvm::cl::desc("Force char to be a signed/unsigned type"));
428
429static llvm::cl::opt<bool>
430DollarsInIdents("fdollars-in-identifiers",
431 llvm::cl::desc("Allow '$' in identifiers"));
432
433static llvm::cl::opt<bool>
434EmitAllDecls("femit-all-decls",
435 llvm::cl::desc("Emit all declarations, even if unused"));
436
437static llvm::cl::opt<bool>
438EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"));
439
440static llvm::cl::opt<bool>
441EnableHeinousExtensions("fheinous-gnu-extensions",
442 llvm::cl::desc("enable GNU extensions that you really really shouldn't use"),
443 llvm::cl::ValueDisallowed, llvm::cl::Hidden);
444
445static llvm::cl::opt<bool>
446Exceptions("fexceptions",
447 llvm::cl::desc("Enable support for exception handling"));
448
449static llvm::cl::opt<bool>
450Freestanding("ffreestanding",
451 llvm::cl::desc("Assert that the compilation takes place in a "
452 "freestanding environment"));
453
454static llvm::cl::opt<bool>
455GNURuntime("fgnu-runtime",
456 llvm::cl::desc("Generate output compatible with the standard GNU "
457 "Objective-C runtime"));
458
459/// LangStds - Language standards we support.
460enum LangStds {
461 lang_unspecified,
462 lang_c89, lang_c94, lang_c99,
463 lang_gnu89, lang_gnu99,
464 lang_cxx98, lang_gnucxx98,
465 lang_cxx0x, lang_gnucxx0x
466};
467static llvm::cl::opt<LangStds>
468LangStd("std", llvm::cl::desc("Language standard to compile for"),
469 llvm::cl::init(lang_unspecified),
470 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
471 clEnumValN(lang_c89, "c90", "ISO C 1990"),
472 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
473 clEnumValN(lang_c94, "iso9899:199409",
474 "ISO C 1990 with amendment 1"),
475 clEnumValN(lang_c99, "c99", "ISO C 1999"),
476 clEnumValN(lang_c99, "c9x", "ISO C 1999"),
477 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
478 clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
479 clEnumValN(lang_gnu89, "gnu89",
480 "ISO C 1990 with GNU extensions"),
481 clEnumValN(lang_gnu99, "gnu99",
482 "ISO C 1999 with GNU extensions (default for C)"),
483 clEnumValN(lang_gnu99, "gnu9x",
484 "ISO C 1999 with GNU extensions"),
485 clEnumValN(lang_cxx98, "c++98",
486 "ISO C++ 1998 with amendments"),
487 clEnumValN(lang_gnucxx98, "gnu++98",
488 "ISO C++ 1998 with amendments and GNU "
489 "extensions (default for C++)"),
490 clEnumValN(lang_cxx0x, "c++0x",
491 "Upcoming ISO C++ 200x with amendments"),
492 clEnumValN(lang_gnucxx0x, "gnu++0x",
493 "Upcoming ISO C++ 200x with amendments and GNU "
494 "extensions"),
495 clEnumValEnd));
496
497static llvm::cl::opt<bool>
498MSExtensions("fms-extensions",
499 llvm::cl::desc("Accept some non-standard constructs used in "
500 "Microsoft header files "));
501
502static llvm::cl::opt<std::string>
503MainFileName("main-file-name",
504 llvm::cl::desc("Main file name to use for debug info"));
505
506static llvm::cl::opt<bool>
507MathErrno("fmath-errno", llvm::cl::init(true),
508 llvm::cl::desc("Require math functions to respect errno"));
509
510static llvm::cl::opt<bool>
511NeXTRuntime("fnext-runtime",
512 llvm::cl::desc("Generate output compatible with the NeXT "
513 "runtime"));
514
515static llvm::cl::opt<bool>
516NoElideConstructors("fno-elide-constructors",
517 llvm::cl::desc("Disable C++ copy constructor elision"));
518
519static llvm::cl::opt<bool>
520NoLaxVectorConversions("fno-lax-vector-conversions",
521 llvm::cl::desc("Disallow implicit conversions between "
522 "vectors with a different number of "
523 "elements or different element types"));
524
525
526static llvm::cl::opt<bool>
527NoOperatorNames("fno-operator-names",
528 llvm::cl::desc("Do not treat C++ operator name keywords as "
529 "synonyms for operators"));
530
531static llvm::cl::opt<std::string>
532ObjCConstantStringClass("fconstant-string-class",
533 llvm::cl::value_desc("class name"),
534 llvm::cl::desc("Specify the class to use for constant "
535 "Objective-C string objects."));
536
537static llvm::cl::opt<bool>
538ObjCEnableGC("fobjc-gc",
539 llvm::cl::desc("Enable Objective-C garbage collection"));
540
541static llvm::cl::opt<bool>
542ObjCExclusiveGC("fobjc-gc-only",
543 llvm::cl::desc("Use GC exclusively for Objective-C related "
544 "memory management"));
545
546static llvm::cl::opt<bool>
547ObjCEnableGCBitmapPrint("print-ivar-layout",
548 llvm::cl::desc("Enable Objective-C Ivar layout bitmap print trace"));
549
550static llvm::cl::opt<bool>
551ObjCNonFragileABI("fobjc-nonfragile-abi",
552 llvm::cl::desc("enable objective-c's nonfragile abi"));
553
554static llvm::cl::opt<bool>
555OverflowChecking("ftrapv",
556 llvm::cl::desc("Trap on integer overflow"),
557 llvm::cl::init(false));
558
559static llvm::cl::opt<unsigned>
560PICLevel("pic-level", llvm::cl::desc("Value for __PIC__"));
561
562static llvm::cl::opt<bool>
563PThread("pthread", llvm::cl::desc("Support POSIX threads in generated code"),
564 llvm::cl::init(false));
565
566static llvm::cl::opt<bool>
567PascalStrings("fpascal-strings",
568 llvm::cl::desc("Recognize and construct Pascal-style "
569 "string literals"));
570
Daniel Dunbar56749082009-11-11 07:26:12 +0000571static llvm::cl::opt<bool>
572Rtti("frtti", llvm::cl::init(true),
573 llvm::cl::desc("Enable generation of rtti information"));
574
575static llvm::cl::opt<bool>
576ShortWChar("fshort-wchar",
577 llvm::cl::desc("Force wchar_t to be a short unsigned int"));
578
579static llvm::cl::opt<bool>
580StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined"));
581
582static llvm::cl::opt<int>
583StackProtector("stack-protector",
584 llvm::cl::desc("Enable stack protectors"),
585 llvm::cl::init(-1));
586
587static llvm::cl::opt<LangOptions::VisibilityMode>
588SymbolVisibility("fvisibility",
589 llvm::cl::desc("Set the default symbol visibility:"),
590 llvm::cl::init(LangOptions::Default),
591 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
592 "Use default symbol visibility"),
593 clEnumValN(LangOptions::Hidden, "hidden",
594 "Use hidden symbol visibility"),
595 clEnumValN(LangOptions::Protected,"protected",
596 "Use protected symbol visibility"),
597 clEnumValEnd));
598
599static llvm::cl::opt<unsigned>
600TemplateDepth("ftemplate-depth", llvm::cl::init(99),
601 llvm::cl::desc("Maximum depth of recursive template "
602 "instantiation"));
603
604static llvm::cl::opt<bool>
605Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences"));
606
607static llvm::cl::opt<bool>
608WritableStrings("fwritable-strings",
609 llvm::cl::desc("Store string literals as writable data"));
610
611}
612
613//===----------------------------------------------------------------------===//
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000614// General Preprocessor Options
615//===----------------------------------------------------------------------===//
616
617namespace preprocessoroptions {
618
619static llvm::cl::list<std::string>
620D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
621 llvm::cl::desc("Predefine the specified macro"));
622
623static llvm::cl::list<std::string>
624ImplicitIncludes("include", llvm::cl::value_desc("file"),
625 llvm::cl::desc("Include file before parsing"));
626static llvm::cl::list<std::string>
627ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"),
628 llvm::cl::desc("Include macros from file before parsing"));
629
630static llvm::cl::opt<std::string>
631ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"),
632 llvm::cl::desc("Include precompiled header file"));
633
634static llvm::cl::opt<std::string>
635ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"),
636 llvm::cl::desc("Include file before parsing"));
637
Daniel Dunbarb3cb98e2009-11-12 02:53:59 +0000638static llvm::cl::opt<std::string>
639TokenCache("token-cache", llvm::cl::value_desc("path"),
640 llvm::cl::desc("Use specified token cache file"));
641
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000642static llvm::cl::list<std::string>
643U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
644 llvm::cl::desc("Undefine the specified macro"));
645
646static llvm::cl::opt<bool>
647UndefMacros("undef", llvm::cl::value_desc("macro"),
648 llvm::cl::desc("undef all system defines"));
649
650}
651
652//===----------------------------------------------------------------------===//
Daniel Dunbarf7973292009-11-11 08:13:32 +0000653// Header Search Options
654//===----------------------------------------------------------------------===//
655
656namespace headersearchoptions {
657
658static llvm::cl::opt<bool>
659nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
660
661static llvm::cl::opt<bool>
662nobuiltininc("nobuiltininc",
663 llvm::cl::desc("Disable builtin #include directories"));
664
665// Various command line options. These four add directories to each chain.
666static llvm::cl::list<std::string>
667F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
668 llvm::cl::desc("Add directory to framework include search path"));
669
670static llvm::cl::list<std::string>
671I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
672 llvm::cl::desc("Add directory to include search path"));
673
674static llvm::cl::list<std::string>
675idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
676 llvm::cl::desc("Add directory to AFTER include search path"));
677
678static llvm::cl::list<std::string>
679iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
680 llvm::cl::desc("Add directory to QUOTE include search path"));
681
682static llvm::cl::list<std::string>
683isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
684 llvm::cl::desc("Add directory to SYSTEM include search path"));
685
686// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
687static llvm::cl::list<std::string>
688iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
689 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
690static llvm::cl::list<std::string>
691iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
692 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
693static llvm::cl::list<std::string>
694iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
695 llvm::cl::Prefix,
696 llvm::cl::desc("Set directory to include search path with prefix"));
697
698static llvm::cl::opt<std::string>
699isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
700 llvm::cl::desc("Set the system root directory (usually /)"));
701
Daniel Dunbar1417c742009-11-12 23:52:46 +0000702static llvm::cl::opt<bool>
703Verbose("v", llvm::cl::desc("Enable verbose output"));
704
Daniel Dunbarf7973292009-11-11 08:13:32 +0000705}
706
707//===----------------------------------------------------------------------===//
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000708// Preprocessed Output Options
709//===----------------------------------------------------------------------===//
710
711namespace preprocessoroutputoptions {
712
713static llvm::cl::opt<bool>
714DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
715
716static llvm::cl::opt<bool>
717EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
718
719static llvm::cl::opt<bool>
720EnableMacroCommentOutput("CC",
721 llvm::cl::desc("Enable comment output in -E mode, "
722 "even from macro expansions"));
723static llvm::cl::opt<bool>
724DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
725 " normal output"));
726static llvm::cl::opt<bool>
727DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
728 "addition to normal output"));
729
730}
731
732//===----------------------------------------------------------------------===//
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000733// Option Object Construction
734//===----------------------------------------------------------------------===//
735
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000736void clang::InitializeCodeGenOptions(CodeGenOptions &Opts,
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000737 const TargetInfo &Target) {
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000738 using namespace codegenoptions;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000739
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000740 // Compute the target features, we need the target to handle this because
741 // features may have dependencies on one another.
742 llvm::StringMap<bool> Features;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000743 Target.getDefaultFeatures(TargetCPU, Features);
744
745 // Apply the user specified deltas.
746 for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
747 ie = TargetFeatures.end(); it != ie; ++it) {
748 const char *Name = it->c_str();
749
750 // FIXME: Don't handle errors like this.
751 if (Name[0] != '-' && Name[0] != '+') {
752 fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
753 Name);
754 exit(1);
755 }
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000756
757 // Apply the feature via the target.
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000758 if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
759 fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
760 Name + 1);
761 exit(1);
762 }
763 }
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000764
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000765 // Add the features to the compile options.
766 //
767 // FIXME: If we are completely confident that we have the right set, we only
768 // need to pass the minuses.
769 for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
770 ie = Features.end(); it != ie; ++it)
771 Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000772
773 // -Os implies -O2
774 Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
775
776 // We must always run at least the always inlining pass.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000777 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
778 : CodeGenOptions::OnlyAlwaysInlining;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000779
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000780 Opts.CPU = TargetCPU;
781 Opts.DebugInfo = GenerateDebugInfo;
782 Opts.DisableLLVMOpts = DisableLLVMOptimizations;
783 Opts.DisableRedZone = DisableRedZone;
784 Opts.MergeAllConstants = !NoMergeConstants;
785 Opts.NoCommon = NoCommon;
786 Opts.NoImplicitFloat = NoImplicitFloat;
787 Opts.OptimizeSize = OptSize;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000788 Opts.SimplifyLibCalls = 1;
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000789 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000790
791#ifdef NDEBUG
792 Opts.VerifyModule = 0;
793#endif
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000794}
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000795
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000796void clang::InitializeDependencyOutputOptions(DependencyOutputOptions &Opts) {
797 using namespace dependencyoutputoptions;
798
799 Opts.OutputFile = DependencyFile;
Daniel Dunbar26266882009-11-12 23:52:32 +0000800 Opts.Targets = DependencyTargets;
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000801 Opts.IncludeSystemHeaders = DependenciesIncludeSystemHeaders;
802 Opts.UsePhonyTargets = PhonyDependencyTarget;
803}
804
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000805void clang::InitializeDiagnosticOptions(DiagnosticOptions &Opts) {
806 using namespace diagnosticoptions;
807
Daniel Dunbar26266882009-11-12 23:52:32 +0000808 Opts.Warnings = OptWarnings;
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000809 Opts.DumpBuildInformation = DumpBuildInformation;
Daniel Dunbar69079432009-11-12 07:28:44 +0000810 Opts.IgnoreWarnings = OptNoWarnings;
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000811 Opts.MessageLength = MessageLength;
Daniel Dunbar69079432009-11-12 07:28:44 +0000812 Opts.NoRewriteMacros = SilenceRewriteMacroWarning;
813 Opts.Pedantic = OptPedantic;
814 Opts.PedanticErrors = OptPedanticErrors;
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000815 Opts.ShowCarets = !NoCaretDiagnostics;
816 Opts.ShowColors = PrintColorDiagnostic;
817 Opts.ShowColumn = !NoShowColumn;
818 Opts.ShowFixits = !NoDiagnosticsFixIt;
819 Opts.ShowLocation = !NoShowLocation;
820 Opts.ShowOptionNames = PrintDiagnosticOption;
821 Opts.ShowSourceRanges = PrintSourceRangeInfo;
Daniel Dunbar26266882009-11-12 23:52:32 +0000822 Opts.VerifyDiagnostics = VerifyDiagnostics;
823}
824
825void clang::InitializeFrontendOptions(FrontendOptions &Opts) {
826 using namespace frontendoptions;
827
Daniel Dunbar914474c2009-11-13 01:02:10 +0000828 Opts.CodeCompletionAt = CodeCompletionAt;
829 Opts.DebugCodeCompletionPrinter = CodeCompletionDebugPrinter;
Daniel Dunbar26266882009-11-12 23:52:32 +0000830 Opts.DisableFree = DisableFree;
831 Opts.EmptyInputOnly = EmptyInputOnly;
Daniel Dunbarc86804b2009-11-12 23:52:56 +0000832 Opts.FixItLocations = FixItAtLocations;
Daniel Dunbar26266882009-11-12 23:52:32 +0000833 Opts.OutputFile = OutputFile;
Daniel Dunbar914474c2009-11-13 01:02:10 +0000834 Opts.RelocatablePCH = RelocatablePCH;
835 Opts.ShowMacrosInCodeCompletion = CodeCompletionWantsMacros;
836 Opts.ShowStats = Stats;
837 Opts.ShowTimers = TimeReport;
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000838 Opts.TargetABI = TargetABI;
839 Opts.TargetTriple = TargetTriple;
Daniel Dunbar26266882009-11-12 23:52:32 +0000840 Opts.ViewClassInheritance = InheritanceViewCls;
841
842 // '-' is the default input if none is given.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000843 if (InputFilenames.empty()) {
844 FrontendOptions::InputKind IK = InputType;
845 if (IK == FrontendOptions::IK_None) IK = FrontendOptions::IK_C;
846 Opts.Inputs.push_back(std::make_pair(IK, "-"));
847 } else {
848 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
849 FrontendOptions::InputKind IK = InputType;
850 llvm::StringRef Ext =
851 llvm::StringRef(InputFilenames[i]).rsplit('.').second;
852 if (IK == FrontendOptions::IK_None)
853 IK = FrontendOptions::getInputKindForExtension(Ext);
854 Opts.Inputs.push_back(std::make_pair(IK, InputFilenames[i]));
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000855 }
856 }
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000857}
858
Daniel Dunbarf7973292009-11-11 08:13:32 +0000859void clang::InitializeHeaderSearchOptions(HeaderSearchOptions &Opts,
860 llvm::StringRef BuiltinIncludePath,
Daniel Dunbarf7973292009-11-11 08:13:32 +0000861 const LangOptions &Lang) {
862 using namespace headersearchoptions;
863
864 Opts.Sysroot = isysroot;
865 Opts.Verbose = Verbose;
866
867 // Handle -I... and -F... options, walking the lists in parallel.
868 unsigned Iidx = 0, Fidx = 0;
869 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
870 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
871 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
872 ++Iidx;
873 } else {
874 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
875 ++Fidx;
876 }
877 }
878
879 // Consume what's left from whatever list was longer.
880 for (; Iidx != I_dirs.size(); ++Iidx)
881 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
882 for (; Fidx != F_dirs.size(); ++Fidx)
883 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
884
885 // Handle -idirafter... options.
886 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
887 Opts.AddPath(idirafter_dirs[i], frontend::After,
888 false, true, false);
889
890 // Handle -iquote... options.
891 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
892 Opts.AddPath(iquote_dirs[i], frontend::Quoted, false, true, false);
893
894 // Handle -isystem... options.
895 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
896 Opts.AddPath(isystem_dirs[i], frontend::System, false, true, false);
897
898 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
899 // parallel, processing the values in order of occurance to get the right
900 // prefixes.
901 {
902 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
903 unsigned iprefix_idx = 0;
904 unsigned iwithprefix_idx = 0;
905 unsigned iwithprefixbefore_idx = 0;
906 bool iprefix_done = iprefix_vals.empty();
907 bool iwithprefix_done = iwithprefix_vals.empty();
908 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
909 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
910 if (!iprefix_done &&
911 (iwithprefix_done ||
912 iprefix_vals.getPosition(iprefix_idx) <
913 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
914 (iwithprefixbefore_done ||
915 iprefix_vals.getPosition(iprefix_idx) <
916 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
917 Prefix = iprefix_vals[iprefix_idx];
918 ++iprefix_idx;
919 iprefix_done = iprefix_idx == iprefix_vals.size();
920 } else if (!iwithprefix_done &&
921 (iwithprefixbefore_done ||
922 iwithprefix_vals.getPosition(iwithprefix_idx) <
923 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
924 Opts.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
925 frontend::System, false, false, false);
926 ++iwithprefix_idx;
927 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
928 } else {
929 Opts.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
930 frontend::Angled, false, false, false);
931 ++iwithprefixbefore_idx;
932 iwithprefixbefore_done =
933 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
934 }
935 }
936 }
937
938 // Add CPATH environment paths.
939 if (const char *Env = getenv("CPATH"))
940 Opts.EnvIncPath = Env;
941
942 // Add language specific environment paths.
943 if (Lang.CPlusPlus && Lang.ObjC1) {
944 if (const char *Env = getenv("OBJCPLUS_INCLUDE_PATH"))
945 Opts.LangEnvIncPath = Env;
946 } else if (Lang.CPlusPlus) {
947 if (const char *Env = getenv("CPLUS_INCLUDE_PATH"))
948 Opts.LangEnvIncPath = Env;
949 } else if (Lang.ObjC1) {
950 if (const char *Env = getenv("OBJC_INCLUDE_PATH"))
951 Opts.LangEnvIncPath = Env;
952 } else {
953 if (const char *Env = getenv("C_INCLUDE_PATH"))
954 Opts.LangEnvIncPath = Env;
955 }
956
957 if (!nobuiltininc)
958 Opts.BuiltinIncludePath = BuiltinIncludePath;
959
960 Opts.UseStandardIncludes = !nostdinc;
961}
962
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000963void clang::InitializePreprocessorOptions(PreprocessorOptions &Opts) {
964 using namespace preprocessoroptions;
965
966 Opts.setImplicitPCHInclude(ImplicitIncludePCH);
967 Opts.setImplicitPTHInclude(ImplicitIncludePTH);
968
Daniel Dunbarb3cb98e2009-11-12 02:53:59 +0000969 // Select the token cache file, we don't support more than one currently so we
970 // can't have both an implicit-pth and a token cache file.
971 if (TokenCache.getPosition() && ImplicitIncludePTH.getPosition()) {
972 // FIXME: Don't fail like this.
973 fprintf(stderr, "error: cannot use both -token-cache and -include-pth "
974 "options\n");
975 exit(1);
976 }
977 if (TokenCache.getPosition())
978 Opts.setTokenCache(TokenCache);
979 else
980 Opts.setTokenCache(ImplicitIncludePTH);
981
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000982 // Use predefines?
983 Opts.setUsePredefines(!UndefMacros);
984
985 // Add macros from the command line.
986 unsigned d = 0, D = D_macros.size();
987 unsigned u = 0, U = U_macros.size();
988 while (d < D || u < U) {
989 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
990 Opts.addMacroDef(D_macros[d++]);
991 else
992 Opts.addMacroUndef(U_macros[u++]);
993 }
994
995 // If -imacros are specified, include them now. These are processed before
996 // any -include directives.
997 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
998 Opts.addMacroInclude(ImplicitMacroIncludes[i]);
999
1000 // Add the ordered list of -includes, sorting in the implicit include options
1001 // at the appropriate location.
1002 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
1003 std::string OriginalFile;
1004
1005 if (!ImplicitIncludePTH.empty())
1006 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
1007 &ImplicitIncludePTH));
1008 if (!ImplicitIncludePCH.empty()) {
1009 OriginalFile = PCHReader::getOriginalSourceFile(ImplicitIncludePCH);
1010 // FIXME: Don't fail like this.
1011 if (OriginalFile.empty())
1012 exit(1);
1013 OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
1014 &OriginalFile));
1015 }
1016 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
1017 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
1018 &ImplicitIncludes[i]));
1019 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
1020
1021 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
1022 Opts.addInclude(*OrderedPaths[i].second);
1023}
Daniel Dunbar56749082009-11-11 07:26:12 +00001024
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001025void clang::InitializeLangOptions(LangOptions &Options,
1026 FrontendOptions::InputKind IK,
Daniel Dunbar56749082009-11-11 07:26:12 +00001027 TargetInfo &Target,
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001028 const CodeGenOptions &CodeGenOpts) {
Daniel Dunbar56749082009-11-11 07:26:12 +00001029 using namespace langoptions;
1030
1031 bool NoPreprocess = false;
1032
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001033 switch (IK) {
1034 case FrontendOptions::IK_None:
1035 case FrontendOptions::IK_AST:
1036 assert(0 && "Invalid input kind!");
1037 case FrontendOptions::IK_Asm:
Daniel Dunbar56749082009-11-11 07:26:12 +00001038 Options.AsmPreprocessor = 1;
1039 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001040 case FrontendOptions::IK_PreprocessedC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001041 NoPreprocess = true;
1042 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001043 case FrontendOptions::IK_C:
Daniel Dunbar56749082009-11-11 07:26:12 +00001044 // Do nothing.
1045 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001046 case FrontendOptions::IK_PreprocessedCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001047 NoPreprocess = true;
1048 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001049 case FrontendOptions::IK_CXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001050 Options.CPlusPlus = 1;
1051 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001052 case FrontendOptions::IK_PreprocessedObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001053 NoPreprocess = true;
1054 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001055 case FrontendOptions::IK_ObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001056 Options.ObjC1 = Options.ObjC2 = 1;
1057 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001058 case FrontendOptions::IK_PreprocessedObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001059 NoPreprocess = true;
1060 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001061 case FrontendOptions::IK_ObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001062 Options.ObjC1 = Options.ObjC2 = 1;
1063 Options.CPlusPlus = 1;
1064 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001065 case FrontendOptions::IK_OpenCL:
Daniel Dunbar56749082009-11-11 07:26:12 +00001066 Options.OpenCL = 1;
1067 Options.AltiVec = 1;
1068 Options.CXXOperatorNames = 1;
1069 Options.LaxVectorConversions = 1;
1070 break;
1071 }
1072
1073 if (ObjCExclusiveGC)
1074 Options.setGCMode(LangOptions::GCOnly);
1075 else if (ObjCEnableGC)
1076 Options.setGCMode(LangOptions::HybridGC);
1077
1078 if (ObjCEnableGCBitmapPrint)
1079 Options.ObjCGCBitmapPrint = 1;
1080
1081 if (AltiVec)
1082 Options.AltiVec = 1;
1083
1084 if (PThread)
1085 Options.POSIXThreads = 1;
1086
1087 Options.setVisibilityMode(SymbolVisibility);
1088 Options.OverflowChecking = OverflowChecking;
1089
1090
1091 // Allow the target to set the default the language options as it sees fit.
1092 Target.getDefaultLangOptions(Options);
1093
1094 // Pass the map of target features to the target for validation and
1095 // processing.
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001096 Target.HandleTargetFeatures(CodeGenOpts.Features);
Daniel Dunbar56749082009-11-11 07:26:12 +00001097
1098 if (LangStd == lang_unspecified) {
1099 // Based on the base language, pick one.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001100 switch (IK) {
1101 case FrontendOptions::IK_None:
1102 case FrontendOptions::IK_AST:
1103 assert(0 && "Invalid input kind!");
1104 case FrontendOptions::IK_OpenCL:
Daniel Dunbar56749082009-11-11 07:26:12 +00001105 LangStd = lang_c99;
1106 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001107 case FrontendOptions::IK_Asm:
1108 case FrontendOptions::IK_C:
1109 case FrontendOptions::IK_PreprocessedC:
1110 case FrontendOptions::IK_ObjC:
1111 case FrontendOptions::IK_PreprocessedObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001112 LangStd = lang_gnu99;
1113 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001114 case FrontendOptions::IK_CXX:
1115 case FrontendOptions::IK_PreprocessedCXX:
1116 case FrontendOptions::IK_ObjCXX:
1117 case FrontendOptions::IK_PreprocessedObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001118 LangStd = lang_gnucxx98;
1119 break;
1120 }
1121 }
1122
1123 switch (LangStd) {
1124 default: assert(0 && "Unknown language standard!");
1125
1126 // Fall through from newer standards to older ones. This isn't really right.
1127 // FIXME: Enable specifically the right features based on the language stds.
1128 case lang_gnucxx0x:
1129 case lang_cxx0x:
1130 Options.CPlusPlus0x = 1;
1131 // FALL THROUGH
1132 case lang_gnucxx98:
1133 case lang_cxx98:
1134 Options.CPlusPlus = 1;
1135 Options.CXXOperatorNames = !NoOperatorNames;
1136 // FALL THROUGH.
1137 case lang_gnu99:
1138 case lang_c99:
1139 Options.C99 = 1;
1140 Options.HexFloats = 1;
1141 // FALL THROUGH.
1142 case lang_gnu89:
1143 Options.BCPLComment = 1; // Only for C99/C++.
1144 // FALL THROUGH.
1145 case lang_c94:
1146 Options.Digraphs = 1; // C94, C99, C++.
1147 // FALL THROUGH.
1148 case lang_c89:
1149 break;
1150 }
1151
1152 // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc.
1153 switch (LangStd) {
1154 default: assert(0 && "Unknown language standard!");
1155 case lang_gnucxx0x:
1156 case lang_gnucxx98:
1157 case lang_gnu99:
1158 case lang_gnu89:
1159 Options.GNUMode = 1;
1160 break;
1161 case lang_cxx0x:
1162 case lang_cxx98:
1163 case lang_c99:
1164 case lang_c94:
1165 case lang_c89:
1166 Options.GNUMode = 0;
1167 break;
1168 }
1169
1170 if (Options.CPlusPlus) {
1171 Options.C99 = 0;
1172 Options.HexFloats = 0;
1173 }
1174
1175 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
1176 Options.ImplicitInt = 1;
1177 else
1178 Options.ImplicitInt = 0;
1179
1180 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
1181 // is specified, or -std is set to a conforming mode.
1182 Options.Trigraphs = !Options.GNUMode;
1183 if (Trigraphs.getPosition())
1184 Options.Trigraphs = Trigraphs; // Command line option wins if specified.
1185
1186 // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off
1187 // even if they are normally on for the target. In GNU modes (e.g.
1188 // -std=gnu99) the default for blocks depends on the target settings.
1189 // However, blocks are not turned off when compiling Obj-C or Obj-C++ code.
1190 if (!Options.ObjC1 && !Options.GNUMode)
1191 Options.Blocks = 0;
1192
1193 // Default to not accepting '$' in identifiers when preprocessing assembler,
1194 // but do accept when preprocessing C. FIXME: these defaults are right for
1195 // darwin, are they right everywhere?
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001196 Options.DollarIdents = IK != FrontendOptions::IK_Asm;
Daniel Dunbar56749082009-11-11 07:26:12 +00001197 if (DollarsInIdents.getPosition()) // Explicit setting overrides default.
1198 Options.DollarIdents = DollarsInIdents;
1199
1200 if (PascalStrings.getPosition())
1201 Options.PascalStrings = PascalStrings;
1202 if (MSExtensions.getPosition())
1203 Options.Microsoft = MSExtensions;
1204 Options.WritableStrings = WritableStrings;
1205 if (NoLaxVectorConversions.getPosition())
1206 Options.LaxVectorConversions = 0;
1207 Options.Exceptions = Exceptions;
1208 Options.Rtti = Rtti;
1209 if (EnableBlocks.getPosition())
1210 Options.Blocks = EnableBlocks;
1211 if (CharIsSigned.getPosition())
1212 Options.CharIsSigned = CharIsSigned;
1213 if (ShortWChar.getPosition())
1214 Options.ShortWChar = ShortWChar;
1215
1216 if (!AllowBuiltins)
1217 Options.NoBuiltin = 1;
1218 if (Freestanding)
1219 Options.Freestanding = Options.NoBuiltin = 1;
1220
1221 if (EnableHeinousExtensions)
1222 Options.HeinousExtensions = 1;
1223
1224 if (AccessControl)
1225 Options.AccessControl = 1;
1226
1227 Options.ElideConstructors = !NoElideConstructors;
1228
1229 // OpenCL and C++ both have bool, true, false keywords.
1230 Options.Bool = Options.OpenCL | Options.CPlusPlus;
1231
1232 Options.MathErrno = MathErrno;
1233
1234 Options.InstantiationDepth = TemplateDepth;
1235
1236 // Override the default runtime if the user requested it.
1237 if (NeXTRuntime)
1238 Options.NeXTRuntime = 1;
1239 else if (GNURuntime)
1240 Options.NeXTRuntime = 0;
1241
1242 if (!ObjCConstantStringClass.empty())
1243 Options.ObjCConstantStringClass = ObjCConstantStringClass.c_str();
1244
1245 if (ObjCNonFragileABI)
1246 Options.ObjCNonFragileABI = 1;
1247
1248 if (EmitAllDecls)
1249 Options.EmitAllDecls = 1;
1250
1251 // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't support.
1252 Options.OptimizeSize = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001253 Options.Optimize = !!CodeGenOpts.OptimizationLevel;
Daniel Dunbar56749082009-11-11 07:26:12 +00001254
1255 assert(PICLevel <= 2 && "Invalid value for -pic-level");
1256 Options.PICLevel = PICLevel;
1257
1258 Options.GNUInline = !Options.C99;
1259 // FIXME: This is affected by other options (-fno-inline).
1260
1261 // This is the __NO_INLINE__ define, which just depends on things like the
1262 // optimization level and -fno-inline, not actually whether the backend has
1263 // inlining enabled.
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001264 Options.NoInline = !CodeGenOpts.OptimizationLevel;
Daniel Dunbar56749082009-11-11 07:26:12 +00001265
1266 Options.Static = StaticDefine;
1267
1268 switch (StackProtector) {
1269 default:
1270 assert(StackProtector <= 2 && "Invalid value for -stack-protector");
1271 case -1: break;
1272 case 0: Options.setStackProtectorMode(LangOptions::SSPOff); break;
1273 case 1: Options.setStackProtectorMode(LangOptions::SSPOn); break;
1274 case 2: Options.setStackProtectorMode(LangOptions::SSPReq); break;
1275 }
1276
1277 if (MainFileName.getPosition())
1278 Options.setMainFileName(MainFileName.c_str());
1279
1280 Target.setForcedLangOptions(Options);
1281}
Daniel Dunbar29cf7462009-11-11 10:07:44 +00001282
1283void
1284clang::InitializePreprocessorOutputOptions(PreprocessorOutputOptions &Opts) {
1285 using namespace preprocessoroutputoptions;
1286
1287 Opts.ShowCPP = !DumpMacros;
1288 Opts.ShowMacros = DumpMacros || DumpDefines;
1289 Opts.ShowLineMarkers = !DisableLineMarkers;
1290 Opts.ShowComments = EnableCommentOutput;
1291 Opts.ShowMacroComments = EnableMacroCommentOutput;
1292}