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