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