blob: 211f1412a1b4d539e4df61cdb21d45b54f8bbd32 [file] [log] [blame]
Daniel Dunbarf89a32a2009-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 Dunbard2cfa012009-11-11 08:13:55 +000015#include "clang/Basic/LangOptions.h"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Frontend/AnalysisConsumer.h"
Daniel Dunbarf89a32a2009-11-10 19:51:53 +000018#include "clang/Frontend/CompileOptions.h"
Daniel Dunbardcd40fb2009-11-11 08:13:40 +000019#include "clang/Frontend/DiagnosticOptions.h"
Daniel Dunbarf527a122009-11-11 08:13:32 +000020#include "clang/Frontend/HeaderSearchOptions.h"
Daniel Dunbar999215c2009-11-11 06:10:03 +000021#include "clang/Frontend/PCHReader.h"
22#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbar22bdabf2009-11-11 10:07:44 +000023#include "clang/Frontend/PreprocessorOutputOptions.h"
Daniel Dunbar999215c2009-11-11 06:10:03 +000024#include "llvm/ADT/STLExtras.h"
Daniel Dunbarf89a32a2009-11-10 19:51:53 +000025#include "llvm/ADT/StringMap.h"
26#include "llvm/Support/CommandLine.h"
Dan Gohmanad5ef3d2009-11-10 21:21:27 +000027#include <stdio.h>
Daniel Dunbarf89a32a2009-11-10 19:51:53 +000028
29using namespace clang;
30
31//===----------------------------------------------------------------------===//
Daniel Dunbard2cfa012009-11-11 08:13:55 +000032// Analyzer Options
33//===----------------------------------------------------------------------===//
34
35namespace analyzeroptions {
36
37static llvm::cl::list<Analyses>
38AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"),
39llvm::cl::values(
40#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
41clEnumValN(NAME, CMDFLAG, DESC),
42#include "clang/Frontend/Analyses.def"
43clEnumValEnd));
44
45static llvm::cl::opt<AnalysisStores>
46AnalysisStoreOpt("analyzer-store",
47 llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"),
48 llvm::cl::init(BasicStoreModel),
49 llvm::cl::values(
50#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\
51clEnumValN(NAME##Model, CMDFLAG, DESC),
52#include "clang/Frontend/Analyses.def"
53clEnumValEnd));
54
55static llvm::cl::opt<AnalysisConstraints>
56AnalysisConstraintsOpt("analyzer-constraints",
57 llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"),
58 llvm::cl::init(RangeConstraintsModel),
59 llvm::cl::values(
60#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\
61clEnumValN(NAME##Model, CMDFLAG, DESC),
62#include "clang/Frontend/Analyses.def"
63clEnumValEnd));
64
65static llvm::cl::opt<AnalysisDiagClients>
66AnalysisDiagOpt("analyzer-output",
67 llvm::cl::desc("Source Code Analysis - Output Options"),
68 llvm::cl::init(PD_HTML),
69 llvm::cl::values(
70#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\
71clEnumValN(PD_##NAME, CMDFLAG, DESC),
72#include "clang/Frontend/Analyses.def"
73clEnumValEnd));
74
75static llvm::cl::opt<bool>
76AnalyzeAll("analyzer-opt-analyze-headers",
77 llvm::cl::desc("Force the static analyzer to analyze "
78 "functions defined in header files"));
79
80static llvm::cl::opt<bool>
81AnalyzerDisplayProgress("analyzer-display-progress",
82 llvm::cl::desc("Emit verbose output about the analyzer's progress."));
83
84static llvm::cl::opt<std::string>
85AnalyzeSpecificFunction("analyze-function",
86 llvm::cl::desc("Run analysis on specific function"));
87
88static llvm::cl::opt<bool>
89EagerlyAssume("analyzer-eagerly-assume",
90 llvm::cl::init(false),
91 llvm::cl::desc("Eagerly assume the truth/falseness of some "
92 "symbolic constraints."));
93
94static llvm::cl::opt<bool>
95PurgeDead("analyzer-purge-dead",
96 llvm::cl::init(true),
97 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
98 " processing a statement."));
99
100static llvm::cl::opt<bool>
101TrimGraph("trim-egraph",
102 llvm::cl::desc("Only show error-related paths in the analysis graph"));
103
104static llvm::cl::opt<bool>
105VisualizeEGDot("analyzer-viz-egraph-graphviz",
106 llvm::cl::desc("Display exploded graph using GraphViz"));
107
108static llvm::cl::opt<bool>
109VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
110 llvm::cl::desc("Display exploded graph using Ubigraph"));
111
112}
113
114void clang::InitializeAnalyzerOptions(AnalyzerOptions &Opts) {
115 using namespace analyzeroptions;
116 Opts.AnalysisList = AnalysisList;
117 Opts.AnalysisStoreOpt = AnalysisStoreOpt;
118 Opts.AnalysisConstraintsOpt = AnalysisConstraintsOpt;
119 Opts.AnalysisDiagOpt = AnalysisDiagOpt;
120 Opts.VisualizeEGDot = VisualizeEGDot;
121 Opts.VisualizeEGUbi = VisualizeEGUbi;
122 Opts.AnalyzeAll = AnalyzeAll;
123 Opts.AnalyzerDisplayProgress = AnalyzerDisplayProgress;
124 Opts.PurgeDead = PurgeDead;
125 Opts.EagerlyAssume = EagerlyAssume;
126 Opts.AnalyzeSpecificFunction = AnalyzeSpecificFunction;
127 Opts.TrimGraph = TrimGraph;
128}
129
130
131//===----------------------------------------------------------------------===//
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000132// Code Generation Options
133//===----------------------------------------------------------------------===//
134
135namespace codegenoptions {
136
137static llvm::cl::opt<bool>
138DisableLLVMOptimizations("disable-llvm-optzns",
139 llvm::cl::desc("Don't run LLVM optimization passes"));
140
141static llvm::cl::opt<bool>
142DisableRedZone("disable-red-zone",
143 llvm::cl::desc("Do not emit code that uses the red zone."),
144 llvm::cl::init(false));
145
146static llvm::cl::opt<bool>
147GenerateDebugInfo("g",
148 llvm::cl::desc("Generate source level debug information"));
149
150static llvm::cl::opt<bool>
151NoCommon("fno-common",
152 llvm::cl::desc("Compile common globals like normal definitions"),
153 llvm::cl::ValueDisallowed);
154
155static llvm::cl::opt<bool>
156NoImplicitFloat("no-implicit-float",
157 llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"),
158 llvm::cl::init(false));
159
160static llvm::cl::opt<bool>
161NoMergeConstants("fno-merge-all-constants",
162 llvm::cl::desc("Disallow merging of constants."));
163
164// It might be nice to add bounds to the CommandLine library directly.
165struct OptLevelParser : public llvm::cl::parser<unsigned> {
166 bool parse(llvm::cl::Option &O, llvm::StringRef ArgName,
167 llvm::StringRef Arg, unsigned &Val) {
168 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
169 return true;
170 if (Val > 3)
171 return O.error("'" + Arg + "' invalid optimization level!");
172 return false;
173 }
174};
175static llvm::cl::opt<unsigned, false, OptLevelParser>
176OptLevel("O", llvm::cl::Prefix,
177 llvm::cl::desc("Optimization level"),
178 llvm::cl::init(0));
179
180static llvm::cl::opt<bool>
181OptSize("Os", llvm::cl::desc("Optimize for size"));
182
183static llvm::cl::opt<std::string>
184TargetCPU("mcpu",
185 llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
186
187static llvm::cl::list<std::string>
188TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes"));
189
190}
191
192//===----------------------------------------------------------------------===//
Daniel Dunbardcd40fb2009-11-11 08:13:40 +0000193// Diagnostic Options
194//===----------------------------------------------------------------------===//
195
196namespace diagnosticoptions {
197
198static llvm::cl::opt<bool>
199NoShowColumn("fno-show-column",
200 llvm::cl::desc("Do not include column number on diagnostics"));
201
202static llvm::cl::opt<bool>
203NoShowLocation("fno-show-source-location",
204 llvm::cl::desc("Do not include source location information with"
205 " diagnostics"));
206
207static llvm::cl::opt<bool>
208NoCaretDiagnostics("fno-caret-diagnostics",
209 llvm::cl::desc("Do not include source line and caret with"
210 " diagnostics"));
211
212static llvm::cl::opt<bool>
213NoDiagnosticsFixIt("fno-diagnostics-fixit-info",
214 llvm::cl::desc("Do not include fixit information in"
215 " diagnostics"));
216
217static llvm::cl::opt<bool>
218PrintSourceRangeInfo("fdiagnostics-print-source-range-info",
219 llvm::cl::desc("Print source range spans in numeric form"));
220
221static llvm::cl::opt<bool>
222PrintDiagnosticOption("fdiagnostics-show-option",
223 llvm::cl::desc("Print diagnostic name with mappable diagnostics"));
224
225static llvm::cl::opt<unsigned>
226MessageLength("fmessage-length",
227 llvm::cl::desc("Format message diagnostics so that they fit "
228 "within N columns or fewer, when possible."),
229 llvm::cl::value_desc("N"));
230
231static llvm::cl::opt<bool>
232PrintColorDiagnostic("fcolor-diagnostics",
233 llvm::cl::desc("Use colors in diagnostics"));
234
235}
236
237//===----------------------------------------------------------------------===//
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000238// Language Options
239//===----------------------------------------------------------------------===//
240
241namespace langoptions {
242
243static llvm::cl::opt<bool>
244AllowBuiltins("fbuiltin", llvm::cl::init(true),
245 llvm::cl::desc("Disable implicit builtin knowledge of functions"));
246
247static llvm::cl::opt<bool>
248AltiVec("faltivec", llvm::cl::desc("Enable AltiVec vector initializer syntax"),
249 llvm::cl::init(false));
250
251static llvm::cl::opt<bool>
252AccessControl("faccess-control",
253 llvm::cl::desc("Enable C++ access control"));
254
255static llvm::cl::opt<bool>
256CharIsSigned("fsigned-char",
257 llvm::cl::desc("Force char to be a signed/unsigned type"));
258
259static llvm::cl::opt<bool>
260DollarsInIdents("fdollars-in-identifiers",
261 llvm::cl::desc("Allow '$' in identifiers"));
262
263static llvm::cl::opt<bool>
264EmitAllDecls("femit-all-decls",
265 llvm::cl::desc("Emit all declarations, even if unused"));
266
267static llvm::cl::opt<bool>
268EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"));
269
270static llvm::cl::opt<bool>
271EnableHeinousExtensions("fheinous-gnu-extensions",
272 llvm::cl::desc("enable GNU extensions that you really really shouldn't use"),
273 llvm::cl::ValueDisallowed, llvm::cl::Hidden);
274
275static llvm::cl::opt<bool>
276Exceptions("fexceptions",
277 llvm::cl::desc("Enable support for exception handling"));
278
279static llvm::cl::opt<bool>
280Freestanding("ffreestanding",
281 llvm::cl::desc("Assert that the compilation takes place in a "
282 "freestanding environment"));
283
284static llvm::cl::opt<bool>
285GNURuntime("fgnu-runtime",
286 llvm::cl::desc("Generate output compatible with the standard GNU "
287 "Objective-C runtime"));
288
289/// LangStds - Language standards we support.
290enum LangStds {
291 lang_unspecified,
292 lang_c89, lang_c94, lang_c99,
293 lang_gnu89, lang_gnu99,
294 lang_cxx98, lang_gnucxx98,
295 lang_cxx0x, lang_gnucxx0x
296};
297static llvm::cl::opt<LangStds>
298LangStd("std", llvm::cl::desc("Language standard to compile for"),
299 llvm::cl::init(lang_unspecified),
300 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
301 clEnumValN(lang_c89, "c90", "ISO C 1990"),
302 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
303 clEnumValN(lang_c94, "iso9899:199409",
304 "ISO C 1990 with amendment 1"),
305 clEnumValN(lang_c99, "c99", "ISO C 1999"),
306 clEnumValN(lang_c99, "c9x", "ISO C 1999"),
307 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
308 clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
309 clEnumValN(lang_gnu89, "gnu89",
310 "ISO C 1990 with GNU extensions"),
311 clEnumValN(lang_gnu99, "gnu99",
312 "ISO C 1999 with GNU extensions (default for C)"),
313 clEnumValN(lang_gnu99, "gnu9x",
314 "ISO C 1999 with GNU extensions"),
315 clEnumValN(lang_cxx98, "c++98",
316 "ISO C++ 1998 with amendments"),
317 clEnumValN(lang_gnucxx98, "gnu++98",
318 "ISO C++ 1998 with amendments and GNU "
319 "extensions (default for C++)"),
320 clEnumValN(lang_cxx0x, "c++0x",
321 "Upcoming ISO C++ 200x with amendments"),
322 clEnumValN(lang_gnucxx0x, "gnu++0x",
323 "Upcoming ISO C++ 200x with amendments and GNU "
324 "extensions"),
325 clEnumValEnd));
326
327static llvm::cl::opt<bool>
328MSExtensions("fms-extensions",
329 llvm::cl::desc("Accept some non-standard constructs used in "
330 "Microsoft header files "));
331
332static llvm::cl::opt<std::string>
333MainFileName("main-file-name",
334 llvm::cl::desc("Main file name to use for debug info"));
335
336static llvm::cl::opt<bool>
337MathErrno("fmath-errno", llvm::cl::init(true),
338 llvm::cl::desc("Require math functions to respect errno"));
339
340static llvm::cl::opt<bool>
341NeXTRuntime("fnext-runtime",
342 llvm::cl::desc("Generate output compatible with the NeXT "
343 "runtime"));
344
345static llvm::cl::opt<bool>
346NoElideConstructors("fno-elide-constructors",
347 llvm::cl::desc("Disable C++ copy constructor elision"));
348
349static llvm::cl::opt<bool>
350NoLaxVectorConversions("fno-lax-vector-conversions",
351 llvm::cl::desc("Disallow implicit conversions between "
352 "vectors with a different number of "
353 "elements or different element types"));
354
355
356static llvm::cl::opt<bool>
357NoOperatorNames("fno-operator-names",
358 llvm::cl::desc("Do not treat C++ operator name keywords as "
359 "synonyms for operators"));
360
361static llvm::cl::opt<std::string>
362ObjCConstantStringClass("fconstant-string-class",
363 llvm::cl::value_desc("class name"),
364 llvm::cl::desc("Specify the class to use for constant "
365 "Objective-C string objects."));
366
367static llvm::cl::opt<bool>
368ObjCEnableGC("fobjc-gc",
369 llvm::cl::desc("Enable Objective-C garbage collection"));
370
371static llvm::cl::opt<bool>
372ObjCExclusiveGC("fobjc-gc-only",
373 llvm::cl::desc("Use GC exclusively for Objective-C related "
374 "memory management"));
375
376static llvm::cl::opt<bool>
377ObjCEnableGCBitmapPrint("print-ivar-layout",
378 llvm::cl::desc("Enable Objective-C Ivar layout bitmap print trace"));
379
380static llvm::cl::opt<bool>
381ObjCNonFragileABI("fobjc-nonfragile-abi",
382 llvm::cl::desc("enable objective-c's nonfragile abi"));
383
384static llvm::cl::opt<bool>
385OverflowChecking("ftrapv",
386 llvm::cl::desc("Trap on integer overflow"),
387 llvm::cl::init(false));
388
389static llvm::cl::opt<unsigned>
390PICLevel("pic-level", llvm::cl::desc("Value for __PIC__"));
391
392static llvm::cl::opt<bool>
393PThread("pthread", llvm::cl::desc("Support POSIX threads in generated code"),
394 llvm::cl::init(false));
395
396static llvm::cl::opt<bool>
397PascalStrings("fpascal-strings",
398 llvm::cl::desc("Recognize and construct Pascal-style "
399 "string literals"));
400
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000401static llvm::cl::opt<bool>
402Rtti("frtti", llvm::cl::init(true),
403 llvm::cl::desc("Enable generation of rtti information"));
404
405static llvm::cl::opt<bool>
406ShortWChar("fshort-wchar",
407 llvm::cl::desc("Force wchar_t to be a short unsigned int"));
408
409static llvm::cl::opt<bool>
410StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined"));
411
412static llvm::cl::opt<int>
413StackProtector("stack-protector",
414 llvm::cl::desc("Enable stack protectors"),
415 llvm::cl::init(-1));
416
417static llvm::cl::opt<LangOptions::VisibilityMode>
418SymbolVisibility("fvisibility",
419 llvm::cl::desc("Set the default symbol visibility:"),
420 llvm::cl::init(LangOptions::Default),
421 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
422 "Use default symbol visibility"),
423 clEnumValN(LangOptions::Hidden, "hidden",
424 "Use hidden symbol visibility"),
425 clEnumValN(LangOptions::Protected,"protected",
426 "Use protected symbol visibility"),
427 clEnumValEnd));
428
429static llvm::cl::opt<unsigned>
430TemplateDepth("ftemplate-depth", llvm::cl::init(99),
431 llvm::cl::desc("Maximum depth of recursive template "
432 "instantiation"));
433
434static llvm::cl::opt<bool>
435Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences"));
436
437static llvm::cl::opt<bool>
438WritableStrings("fwritable-strings",
439 llvm::cl::desc("Store string literals as writable data"));
440
441}
442
443//===----------------------------------------------------------------------===//
Daniel Dunbar999215c2009-11-11 06:10:03 +0000444// General Preprocessor Options
445//===----------------------------------------------------------------------===//
446
447namespace preprocessoroptions {
448
449static llvm::cl::list<std::string>
450D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
451 llvm::cl::desc("Predefine the specified macro"));
452
453static llvm::cl::list<std::string>
454ImplicitIncludes("include", llvm::cl::value_desc("file"),
455 llvm::cl::desc("Include file before parsing"));
456static llvm::cl::list<std::string>
457ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"),
458 llvm::cl::desc("Include macros from file before parsing"));
459
460static llvm::cl::opt<std::string>
461ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"),
462 llvm::cl::desc("Include precompiled header file"));
463
464static llvm::cl::opt<std::string>
465ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"),
466 llvm::cl::desc("Include file before parsing"));
467
468static llvm::cl::list<std::string>
469U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
470 llvm::cl::desc("Undefine the specified macro"));
471
472static llvm::cl::opt<bool>
473UndefMacros("undef", llvm::cl::value_desc("macro"),
474 llvm::cl::desc("undef all system defines"));
475
476}
477
478//===----------------------------------------------------------------------===//
Daniel Dunbarf527a122009-11-11 08:13:32 +0000479// Header Search Options
480//===----------------------------------------------------------------------===//
481
482namespace headersearchoptions {
483
484static llvm::cl::opt<bool>
485nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
486
487static llvm::cl::opt<bool>
488nobuiltininc("nobuiltininc",
489 llvm::cl::desc("Disable builtin #include directories"));
490
491// Various command line options. These four add directories to each chain.
492static llvm::cl::list<std::string>
493F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
494 llvm::cl::desc("Add directory to framework include search path"));
495
496static llvm::cl::list<std::string>
497I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
498 llvm::cl::desc("Add directory to include search path"));
499
500static llvm::cl::list<std::string>
501idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
502 llvm::cl::desc("Add directory to AFTER include search path"));
503
504static llvm::cl::list<std::string>
505iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
506 llvm::cl::desc("Add directory to QUOTE include search path"));
507
508static llvm::cl::list<std::string>
509isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
510 llvm::cl::desc("Add directory to SYSTEM include search path"));
511
512// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
513static llvm::cl::list<std::string>
514iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
515 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
516static llvm::cl::list<std::string>
517iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
518 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
519static llvm::cl::list<std::string>
520iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
521 llvm::cl::Prefix,
522 llvm::cl::desc("Set directory to include search path with prefix"));
523
524static llvm::cl::opt<std::string>
525isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
526 llvm::cl::desc("Set the system root directory (usually /)"));
527
528}
529
530//===----------------------------------------------------------------------===//
Daniel Dunbar22bdabf2009-11-11 10:07:44 +0000531// Preprocessed Output Options
532//===----------------------------------------------------------------------===//
533
534namespace preprocessoroutputoptions {
535
536static llvm::cl::opt<bool>
537DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
538
539static llvm::cl::opt<bool>
540EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
541
542static llvm::cl::opt<bool>
543EnableMacroCommentOutput("CC",
544 llvm::cl::desc("Enable comment output in -E mode, "
545 "even from macro expansions"));
546static llvm::cl::opt<bool>
547DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
548 " normal output"));
549static llvm::cl::opt<bool>
550DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
551 "addition to normal output"));
552
553}
554
555//===----------------------------------------------------------------------===//
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000556// Option Object Construction
557//===----------------------------------------------------------------------===//
558
Daniel Dunbar979586e2009-11-11 09:38:56 +0000559void clang::InitializeCompileOptions(CompileOptions &Opts,
560 const TargetInfo &Target) {
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000561 using namespace codegenoptions;
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000562
Daniel Dunbar979586e2009-11-11 09:38:56 +0000563 // Compute the target features, we need the target to handle this because
564 // features may have dependencies on one another.
565 llvm::StringMap<bool> Features;
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000566 Target.getDefaultFeatures(TargetCPU, Features);
567
568 // Apply the user specified deltas.
569 for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
570 ie = TargetFeatures.end(); it != ie; ++it) {
571 const char *Name = it->c_str();
572
573 // FIXME: Don't handle errors like this.
574 if (Name[0] != '-' && Name[0] != '+') {
575 fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
576 Name);
577 exit(1);
578 }
Daniel Dunbar979586e2009-11-11 09:38:56 +0000579
580 // Apply the feature via the target.
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000581 if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
582 fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
583 Name + 1);
584 exit(1);
585 }
586 }
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000587
Daniel Dunbar979586e2009-11-11 09:38:56 +0000588 // Add the features to the compile options.
589 //
590 // FIXME: If we are completely confident that we have the right set, we only
591 // need to pass the minuses.
592 for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
593 ie = Features.end(); it != ie; ++it)
594 Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000595
596 // -Os implies -O2
597 Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
598
599 // We must always run at least the always inlining pass.
600 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CompileOptions::NormalInlining
601 : CompileOptions::OnlyAlwaysInlining;
602
Daniel Dunbar979586e2009-11-11 09:38:56 +0000603 Opts.CPU = TargetCPU;
604 Opts.DebugInfo = GenerateDebugInfo;
605 Opts.DisableLLVMOpts = DisableLLVMOptimizations;
606 Opts.DisableRedZone = DisableRedZone;
607 Opts.MergeAllConstants = !NoMergeConstants;
608 Opts.NoCommon = NoCommon;
609 Opts.NoImplicitFloat = NoImplicitFloat;
610 Opts.OptimizeSize = OptSize;
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000611 Opts.SimplifyLibCalls = 1;
Daniel Dunbar979586e2009-11-11 09:38:56 +0000612 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000613
614#ifdef NDEBUG
615 Opts.VerifyModule = 0;
616#endif
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000617}
Daniel Dunbar999215c2009-11-11 06:10:03 +0000618
Daniel Dunbardcd40fb2009-11-11 08:13:40 +0000619void clang::InitializeDiagnosticOptions(DiagnosticOptions &Opts) {
620 using namespace diagnosticoptions;
621
622 Opts.ShowColumn = !NoShowColumn;
623 Opts.ShowLocation = !NoShowLocation;
624 Opts.ShowCarets = !NoCaretDiagnostics;
625 Opts.ShowFixits = !NoDiagnosticsFixIt;
626 Opts.ShowSourceRanges = PrintSourceRangeInfo;
627 Opts.ShowOptionNames = PrintDiagnosticOption;
628 Opts.ShowColors = PrintColorDiagnostic;
629 Opts.MessageLength = MessageLength;
630}
631
Daniel Dunbarf527a122009-11-11 08:13:32 +0000632void clang::InitializeHeaderSearchOptions(HeaderSearchOptions &Opts,
633 llvm::StringRef BuiltinIncludePath,
634 bool Verbose,
635 const LangOptions &Lang) {
636 using namespace headersearchoptions;
637
638 Opts.Sysroot = isysroot;
639 Opts.Verbose = Verbose;
640
641 // Handle -I... and -F... options, walking the lists in parallel.
642 unsigned Iidx = 0, Fidx = 0;
643 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
644 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
645 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
646 ++Iidx;
647 } else {
648 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
649 ++Fidx;
650 }
651 }
652
653 // Consume what's left from whatever list was longer.
654 for (; Iidx != I_dirs.size(); ++Iidx)
655 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
656 for (; Fidx != F_dirs.size(); ++Fidx)
657 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
658
659 // Handle -idirafter... options.
660 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
661 Opts.AddPath(idirafter_dirs[i], frontend::After,
662 false, true, false);
663
664 // Handle -iquote... options.
665 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
666 Opts.AddPath(iquote_dirs[i], frontend::Quoted, false, true, false);
667
668 // Handle -isystem... options.
669 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
670 Opts.AddPath(isystem_dirs[i], frontend::System, false, true, false);
671
672 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
673 // parallel, processing the values in order of occurance to get the right
674 // prefixes.
675 {
676 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
677 unsigned iprefix_idx = 0;
678 unsigned iwithprefix_idx = 0;
679 unsigned iwithprefixbefore_idx = 0;
680 bool iprefix_done = iprefix_vals.empty();
681 bool iwithprefix_done = iwithprefix_vals.empty();
682 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
683 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
684 if (!iprefix_done &&
685 (iwithprefix_done ||
686 iprefix_vals.getPosition(iprefix_idx) <
687 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
688 (iwithprefixbefore_done ||
689 iprefix_vals.getPosition(iprefix_idx) <
690 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
691 Prefix = iprefix_vals[iprefix_idx];
692 ++iprefix_idx;
693 iprefix_done = iprefix_idx == iprefix_vals.size();
694 } else if (!iwithprefix_done &&
695 (iwithprefixbefore_done ||
696 iwithprefix_vals.getPosition(iwithprefix_idx) <
697 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
698 Opts.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
699 frontend::System, false, false, false);
700 ++iwithprefix_idx;
701 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
702 } else {
703 Opts.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
704 frontend::Angled, false, false, false);
705 ++iwithprefixbefore_idx;
706 iwithprefixbefore_done =
707 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
708 }
709 }
710 }
711
712 // Add CPATH environment paths.
713 if (const char *Env = getenv("CPATH"))
714 Opts.EnvIncPath = Env;
715
716 // Add language specific environment paths.
717 if (Lang.CPlusPlus && Lang.ObjC1) {
718 if (const char *Env = getenv("OBJCPLUS_INCLUDE_PATH"))
719 Opts.LangEnvIncPath = Env;
720 } else if (Lang.CPlusPlus) {
721 if (const char *Env = getenv("CPLUS_INCLUDE_PATH"))
722 Opts.LangEnvIncPath = Env;
723 } else if (Lang.ObjC1) {
724 if (const char *Env = getenv("OBJC_INCLUDE_PATH"))
725 Opts.LangEnvIncPath = Env;
726 } else {
727 if (const char *Env = getenv("C_INCLUDE_PATH"))
728 Opts.LangEnvIncPath = Env;
729 }
730
731 if (!nobuiltininc)
732 Opts.BuiltinIncludePath = BuiltinIncludePath;
733
734 Opts.UseStandardIncludes = !nostdinc;
735}
736
Daniel Dunbar999215c2009-11-11 06:10:03 +0000737void clang::InitializePreprocessorOptions(PreprocessorOptions &Opts) {
738 using namespace preprocessoroptions;
739
740 Opts.setImplicitPCHInclude(ImplicitIncludePCH);
741 Opts.setImplicitPTHInclude(ImplicitIncludePTH);
742
743 // Use predefines?
744 Opts.setUsePredefines(!UndefMacros);
745
746 // Add macros from the command line.
747 unsigned d = 0, D = D_macros.size();
748 unsigned u = 0, U = U_macros.size();
749 while (d < D || u < U) {
750 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
751 Opts.addMacroDef(D_macros[d++]);
752 else
753 Opts.addMacroUndef(U_macros[u++]);
754 }
755
756 // If -imacros are specified, include them now. These are processed before
757 // any -include directives.
758 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
759 Opts.addMacroInclude(ImplicitMacroIncludes[i]);
760
761 // Add the ordered list of -includes, sorting in the implicit include options
762 // at the appropriate location.
763 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
764 std::string OriginalFile;
765
766 if (!ImplicitIncludePTH.empty())
767 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
768 &ImplicitIncludePTH));
769 if (!ImplicitIncludePCH.empty()) {
770 OriginalFile = PCHReader::getOriginalSourceFile(ImplicitIncludePCH);
771 // FIXME: Don't fail like this.
772 if (OriginalFile.empty())
773 exit(1);
774 OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
775 &OriginalFile));
776 }
777 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
778 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
779 &ImplicitIncludes[i]));
780 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
781
782 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
783 Opts.addInclude(*OrderedPaths[i].second);
784}
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000785
786void clang::InitializeLangOptions(LangOptions &Options, LangKind LK,
787 TargetInfo &Target,
Daniel Dunbar979586e2009-11-11 09:38:56 +0000788 const CompileOptions &CompileOpts) {
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000789 using namespace langoptions;
790
791 bool NoPreprocess = false;
792
793 switch (LK) {
794 default: assert(0 && "Unknown language kind!");
795 case langkind_asm_cpp:
796 Options.AsmPreprocessor = 1;
797 // FALLTHROUGH
798 case langkind_c_cpp:
799 NoPreprocess = true;
800 // FALLTHROUGH
801 case langkind_c:
802 // Do nothing.
803 break;
804 case langkind_cxx_cpp:
805 NoPreprocess = true;
806 // FALLTHROUGH
807 case langkind_cxx:
808 Options.CPlusPlus = 1;
809 break;
810 case langkind_objc_cpp:
811 NoPreprocess = true;
812 // FALLTHROUGH
813 case langkind_objc:
814 Options.ObjC1 = Options.ObjC2 = 1;
815 break;
816 case langkind_objcxx_cpp:
817 NoPreprocess = true;
818 // FALLTHROUGH
819 case langkind_objcxx:
820 Options.ObjC1 = Options.ObjC2 = 1;
821 Options.CPlusPlus = 1;
822 break;
823 case langkind_ocl:
824 Options.OpenCL = 1;
825 Options.AltiVec = 1;
826 Options.CXXOperatorNames = 1;
827 Options.LaxVectorConversions = 1;
828 break;
829 }
830
831 if (ObjCExclusiveGC)
832 Options.setGCMode(LangOptions::GCOnly);
833 else if (ObjCEnableGC)
834 Options.setGCMode(LangOptions::HybridGC);
835
836 if (ObjCEnableGCBitmapPrint)
837 Options.ObjCGCBitmapPrint = 1;
838
839 if (AltiVec)
840 Options.AltiVec = 1;
841
842 if (PThread)
843 Options.POSIXThreads = 1;
844
845 Options.setVisibilityMode(SymbolVisibility);
846 Options.OverflowChecking = OverflowChecking;
847
848
849 // Allow the target to set the default the language options as it sees fit.
850 Target.getDefaultLangOptions(Options);
851
852 // Pass the map of target features to the target for validation and
853 // processing.
Daniel Dunbar979586e2009-11-11 09:38:56 +0000854 Target.HandleTargetFeatures(CompileOpts.Features);
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000855
856 if (LangStd == lang_unspecified) {
857 // Based on the base language, pick one.
858 switch (LK) {
859 case langkind_ast: assert(0 && "Invalid call for AST inputs");
860 case lang_unspecified: assert(0 && "Unknown base language");
861 case langkind_ocl:
862 LangStd = lang_c99;
863 break;
864 case langkind_c:
865 case langkind_asm_cpp:
866 case langkind_c_cpp:
867 case langkind_objc:
868 case langkind_objc_cpp:
869 LangStd = lang_gnu99;
870 break;
871 case langkind_cxx:
872 case langkind_cxx_cpp:
873 case langkind_objcxx:
874 case langkind_objcxx_cpp:
875 LangStd = lang_gnucxx98;
876 break;
877 }
878 }
879
880 switch (LangStd) {
881 default: assert(0 && "Unknown language standard!");
882
883 // Fall through from newer standards to older ones. This isn't really right.
884 // FIXME: Enable specifically the right features based on the language stds.
885 case lang_gnucxx0x:
886 case lang_cxx0x:
887 Options.CPlusPlus0x = 1;
888 // FALL THROUGH
889 case lang_gnucxx98:
890 case lang_cxx98:
891 Options.CPlusPlus = 1;
892 Options.CXXOperatorNames = !NoOperatorNames;
893 // FALL THROUGH.
894 case lang_gnu99:
895 case lang_c99:
896 Options.C99 = 1;
897 Options.HexFloats = 1;
898 // FALL THROUGH.
899 case lang_gnu89:
900 Options.BCPLComment = 1; // Only for C99/C++.
901 // FALL THROUGH.
902 case lang_c94:
903 Options.Digraphs = 1; // C94, C99, C++.
904 // FALL THROUGH.
905 case lang_c89:
906 break;
907 }
908
909 // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc.
910 switch (LangStd) {
911 default: assert(0 && "Unknown language standard!");
912 case lang_gnucxx0x:
913 case lang_gnucxx98:
914 case lang_gnu99:
915 case lang_gnu89:
916 Options.GNUMode = 1;
917 break;
918 case lang_cxx0x:
919 case lang_cxx98:
920 case lang_c99:
921 case lang_c94:
922 case lang_c89:
923 Options.GNUMode = 0;
924 break;
925 }
926
927 if (Options.CPlusPlus) {
928 Options.C99 = 0;
929 Options.HexFloats = 0;
930 }
931
932 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
933 Options.ImplicitInt = 1;
934 else
935 Options.ImplicitInt = 0;
936
937 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
938 // is specified, or -std is set to a conforming mode.
939 Options.Trigraphs = !Options.GNUMode;
940 if (Trigraphs.getPosition())
941 Options.Trigraphs = Trigraphs; // Command line option wins if specified.
942
943 // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off
944 // even if they are normally on for the target. In GNU modes (e.g.
945 // -std=gnu99) the default for blocks depends on the target settings.
946 // However, blocks are not turned off when compiling Obj-C or Obj-C++ code.
947 if (!Options.ObjC1 && !Options.GNUMode)
948 Options.Blocks = 0;
949
950 // Default to not accepting '$' in identifiers when preprocessing assembler,
951 // but do accept when preprocessing C. FIXME: these defaults are right for
952 // darwin, are they right everywhere?
953 Options.DollarIdents = LK != langkind_asm_cpp;
954 if (DollarsInIdents.getPosition()) // Explicit setting overrides default.
955 Options.DollarIdents = DollarsInIdents;
956
957 if (PascalStrings.getPosition())
958 Options.PascalStrings = PascalStrings;
959 if (MSExtensions.getPosition())
960 Options.Microsoft = MSExtensions;
961 Options.WritableStrings = WritableStrings;
962 if (NoLaxVectorConversions.getPosition())
963 Options.LaxVectorConversions = 0;
964 Options.Exceptions = Exceptions;
965 Options.Rtti = Rtti;
966 if (EnableBlocks.getPosition())
967 Options.Blocks = EnableBlocks;
968 if (CharIsSigned.getPosition())
969 Options.CharIsSigned = CharIsSigned;
970 if (ShortWChar.getPosition())
971 Options.ShortWChar = ShortWChar;
972
973 if (!AllowBuiltins)
974 Options.NoBuiltin = 1;
975 if (Freestanding)
976 Options.Freestanding = Options.NoBuiltin = 1;
977
978 if (EnableHeinousExtensions)
979 Options.HeinousExtensions = 1;
980
981 if (AccessControl)
982 Options.AccessControl = 1;
983
984 Options.ElideConstructors = !NoElideConstructors;
985
986 // OpenCL and C++ both have bool, true, false keywords.
987 Options.Bool = Options.OpenCL | Options.CPlusPlus;
988
989 Options.MathErrno = MathErrno;
990
991 Options.InstantiationDepth = TemplateDepth;
992
993 // Override the default runtime if the user requested it.
994 if (NeXTRuntime)
995 Options.NeXTRuntime = 1;
996 else if (GNURuntime)
997 Options.NeXTRuntime = 0;
998
999 if (!ObjCConstantStringClass.empty())
1000 Options.ObjCConstantStringClass = ObjCConstantStringClass.c_str();
1001
1002 if (ObjCNonFragileABI)
1003 Options.ObjCNonFragileABI = 1;
1004
1005 if (EmitAllDecls)
1006 Options.EmitAllDecls = 1;
1007
1008 // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't support.
1009 Options.OptimizeSize = 0;
1010 Options.Optimize = !!CompileOpts.OptimizationLevel;
1011
1012 assert(PICLevel <= 2 && "Invalid value for -pic-level");
1013 Options.PICLevel = PICLevel;
1014
1015 Options.GNUInline = !Options.C99;
1016 // FIXME: This is affected by other options (-fno-inline).
1017
1018 // This is the __NO_INLINE__ define, which just depends on things like the
1019 // optimization level and -fno-inline, not actually whether the backend has
1020 // inlining enabled.
1021 Options.NoInline = !CompileOpts.OptimizationLevel;
1022
1023 Options.Static = StaticDefine;
1024
1025 switch (StackProtector) {
1026 default:
1027 assert(StackProtector <= 2 && "Invalid value for -stack-protector");
1028 case -1: break;
1029 case 0: Options.setStackProtectorMode(LangOptions::SSPOff); break;
1030 case 1: Options.setStackProtectorMode(LangOptions::SSPOn); break;
1031 case 2: Options.setStackProtectorMode(LangOptions::SSPReq); break;
1032 }
1033
1034 if (MainFileName.getPosition())
1035 Options.setMainFileName(MainFileName.c_str());
1036
1037 Target.setForcedLangOptions(Options);
1038}
Daniel Dunbar22bdabf2009-11-11 10:07:44 +00001039
1040void
1041clang::InitializePreprocessorOutputOptions(PreprocessorOutputOptions &Opts) {
1042 using namespace preprocessoroutputoptions;
1043
1044 Opts.ShowCPP = !DumpMacros;
1045 Opts.ShowMacros = DumpMacros || DumpDefines;
1046 Opts.ShowLineMarkers = !DisableLineMarkers;
1047 Opts.ShowComments = EnableCommentOutput;
1048 Opts.ShowMacroComments = EnableMacroCommentOutput;
1049}
1050