blob: e2b493148bcdd10a0fa40386236bd55b97e13194 [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
Daniel Dunbar29403032009-11-12 02:53:59 +0000494static llvm::cl::opt<std::string>
495TokenCache("token-cache", llvm::cl::value_desc("path"),
496 llvm::cl::desc("Use specified token cache file"));
497
Daniel Dunbar999215c2009-11-11 06:10:03 +0000498static llvm::cl::list<std::string>
499U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
500 llvm::cl::desc("Undefine the specified macro"));
501
502static llvm::cl::opt<bool>
503UndefMacros("undef", llvm::cl::value_desc("macro"),
504 llvm::cl::desc("undef all system defines"));
505
506}
507
508//===----------------------------------------------------------------------===//
Daniel Dunbarf527a122009-11-11 08:13:32 +0000509// Header Search Options
510//===----------------------------------------------------------------------===//
511
512namespace headersearchoptions {
513
514static llvm::cl::opt<bool>
515nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
516
517static llvm::cl::opt<bool>
518nobuiltininc("nobuiltininc",
519 llvm::cl::desc("Disable builtin #include directories"));
520
521// Various command line options. These four add directories to each chain.
522static llvm::cl::list<std::string>
523F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
524 llvm::cl::desc("Add directory to framework include search path"));
525
526static llvm::cl::list<std::string>
527I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
528 llvm::cl::desc("Add directory to include search path"));
529
530static llvm::cl::list<std::string>
531idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
532 llvm::cl::desc("Add directory to AFTER include search path"));
533
534static llvm::cl::list<std::string>
535iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
536 llvm::cl::desc("Add directory to QUOTE include search path"));
537
538static llvm::cl::list<std::string>
539isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
540 llvm::cl::desc("Add directory to SYSTEM include search path"));
541
542// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
543static llvm::cl::list<std::string>
544iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
545 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
546static llvm::cl::list<std::string>
547iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
548 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
549static llvm::cl::list<std::string>
550iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
551 llvm::cl::Prefix,
552 llvm::cl::desc("Set directory to include search path with prefix"));
553
554static llvm::cl::opt<std::string>
555isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
556 llvm::cl::desc("Set the system root directory (usually /)"));
557
558}
559
560//===----------------------------------------------------------------------===//
Daniel Dunbar22bdabf2009-11-11 10:07:44 +0000561// Preprocessed Output Options
562//===----------------------------------------------------------------------===//
563
564namespace preprocessoroutputoptions {
565
566static llvm::cl::opt<bool>
567DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
568
569static llvm::cl::opt<bool>
570EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
571
572static llvm::cl::opt<bool>
573EnableMacroCommentOutput("CC",
574 llvm::cl::desc("Enable comment output in -E mode, "
575 "even from macro expansions"));
576static llvm::cl::opt<bool>
577DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
578 " normal output"));
579static llvm::cl::opt<bool>
580DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
581 "addition to normal output"));
582
583}
584
585//===----------------------------------------------------------------------===//
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000586// Option Object Construction
587//===----------------------------------------------------------------------===//
588
Daniel Dunbar979586e2009-11-11 09:38:56 +0000589void clang::InitializeCompileOptions(CompileOptions &Opts,
590 const TargetInfo &Target) {
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000591 using namespace codegenoptions;
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000592
Daniel Dunbar979586e2009-11-11 09:38:56 +0000593 // Compute the target features, we need the target to handle this because
594 // features may have dependencies on one another.
595 llvm::StringMap<bool> Features;
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000596 Target.getDefaultFeatures(TargetCPU, Features);
597
598 // Apply the user specified deltas.
599 for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
600 ie = TargetFeatures.end(); it != ie; ++it) {
601 const char *Name = it->c_str();
602
603 // FIXME: Don't handle errors like this.
604 if (Name[0] != '-' && Name[0] != '+') {
605 fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
606 Name);
607 exit(1);
608 }
Daniel Dunbar979586e2009-11-11 09:38:56 +0000609
610 // Apply the feature via the target.
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000611 if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
612 fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
613 Name + 1);
614 exit(1);
615 }
616 }
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000617
Daniel Dunbar979586e2009-11-11 09:38:56 +0000618 // Add the features to the compile options.
619 //
620 // FIXME: If we are completely confident that we have the right set, we only
621 // need to pass the minuses.
622 for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
623 ie = Features.end(); it != ie; ++it)
624 Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000625
626 // -Os implies -O2
627 Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
628
629 // We must always run at least the always inlining pass.
630 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CompileOptions::NormalInlining
631 : CompileOptions::OnlyAlwaysInlining;
632
Daniel Dunbar979586e2009-11-11 09:38:56 +0000633 Opts.CPU = TargetCPU;
634 Opts.DebugInfo = GenerateDebugInfo;
635 Opts.DisableLLVMOpts = DisableLLVMOptimizations;
636 Opts.DisableRedZone = DisableRedZone;
637 Opts.MergeAllConstants = !NoMergeConstants;
638 Opts.NoCommon = NoCommon;
639 Opts.NoImplicitFloat = NoImplicitFloat;
640 Opts.OptimizeSize = OptSize;
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000641 Opts.SimplifyLibCalls = 1;
Daniel Dunbar979586e2009-11-11 09:38:56 +0000642 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000643
644#ifdef NDEBUG
645 Opts.VerifyModule = 0;
646#endif
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000647}
Daniel Dunbar999215c2009-11-11 06:10:03 +0000648
Daniel Dunbar89d1fdf2009-11-11 21:43:12 +0000649void clang::InitializeDependencyOutputOptions(DependencyOutputOptions &Opts) {
650 using namespace dependencyoutputoptions;
651
652 Opts.OutputFile = DependencyFile;
653 Opts.Targets.insert(Opts.Targets.begin(), DependencyTargets.begin(),
654 DependencyTargets.end());
655 Opts.IncludeSystemHeaders = DependenciesIncludeSystemHeaders;
656 Opts.UsePhonyTargets = PhonyDependencyTarget;
657}
658
Daniel Dunbardcd40fb2009-11-11 08:13:40 +0000659void clang::InitializeDiagnosticOptions(DiagnosticOptions &Opts) {
660 using namespace diagnosticoptions;
661
662 Opts.ShowColumn = !NoShowColumn;
663 Opts.ShowLocation = !NoShowLocation;
664 Opts.ShowCarets = !NoCaretDiagnostics;
665 Opts.ShowFixits = !NoDiagnosticsFixIt;
666 Opts.ShowSourceRanges = PrintSourceRangeInfo;
667 Opts.ShowOptionNames = PrintDiagnosticOption;
668 Opts.ShowColors = PrintColorDiagnostic;
669 Opts.MessageLength = MessageLength;
670}
671
Daniel Dunbarf527a122009-11-11 08:13:32 +0000672void clang::InitializeHeaderSearchOptions(HeaderSearchOptions &Opts,
673 llvm::StringRef BuiltinIncludePath,
674 bool Verbose,
675 const LangOptions &Lang) {
676 using namespace headersearchoptions;
677
678 Opts.Sysroot = isysroot;
679 Opts.Verbose = Verbose;
680
681 // Handle -I... and -F... options, walking the lists in parallel.
682 unsigned Iidx = 0, Fidx = 0;
683 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
684 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
685 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
686 ++Iidx;
687 } else {
688 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
689 ++Fidx;
690 }
691 }
692
693 // Consume what's left from whatever list was longer.
694 for (; Iidx != I_dirs.size(); ++Iidx)
695 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
696 for (; Fidx != F_dirs.size(); ++Fidx)
697 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
698
699 // Handle -idirafter... options.
700 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
701 Opts.AddPath(idirafter_dirs[i], frontend::After,
702 false, true, false);
703
704 // Handle -iquote... options.
705 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
706 Opts.AddPath(iquote_dirs[i], frontend::Quoted, false, true, false);
707
708 // Handle -isystem... options.
709 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
710 Opts.AddPath(isystem_dirs[i], frontend::System, false, true, false);
711
712 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
713 // parallel, processing the values in order of occurance to get the right
714 // prefixes.
715 {
716 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
717 unsigned iprefix_idx = 0;
718 unsigned iwithprefix_idx = 0;
719 unsigned iwithprefixbefore_idx = 0;
720 bool iprefix_done = iprefix_vals.empty();
721 bool iwithprefix_done = iwithprefix_vals.empty();
722 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
723 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
724 if (!iprefix_done &&
725 (iwithprefix_done ||
726 iprefix_vals.getPosition(iprefix_idx) <
727 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
728 (iwithprefixbefore_done ||
729 iprefix_vals.getPosition(iprefix_idx) <
730 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
731 Prefix = iprefix_vals[iprefix_idx];
732 ++iprefix_idx;
733 iprefix_done = iprefix_idx == iprefix_vals.size();
734 } else if (!iwithprefix_done &&
735 (iwithprefixbefore_done ||
736 iwithprefix_vals.getPosition(iwithprefix_idx) <
737 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
738 Opts.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
739 frontend::System, false, false, false);
740 ++iwithprefix_idx;
741 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
742 } else {
743 Opts.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
744 frontend::Angled, false, false, false);
745 ++iwithprefixbefore_idx;
746 iwithprefixbefore_done =
747 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
748 }
749 }
750 }
751
752 // Add CPATH environment paths.
753 if (const char *Env = getenv("CPATH"))
754 Opts.EnvIncPath = Env;
755
756 // Add language specific environment paths.
757 if (Lang.CPlusPlus && Lang.ObjC1) {
758 if (const char *Env = getenv("OBJCPLUS_INCLUDE_PATH"))
759 Opts.LangEnvIncPath = Env;
760 } else if (Lang.CPlusPlus) {
761 if (const char *Env = getenv("CPLUS_INCLUDE_PATH"))
762 Opts.LangEnvIncPath = Env;
763 } else if (Lang.ObjC1) {
764 if (const char *Env = getenv("OBJC_INCLUDE_PATH"))
765 Opts.LangEnvIncPath = Env;
766 } else {
767 if (const char *Env = getenv("C_INCLUDE_PATH"))
768 Opts.LangEnvIncPath = Env;
769 }
770
771 if (!nobuiltininc)
772 Opts.BuiltinIncludePath = BuiltinIncludePath;
773
774 Opts.UseStandardIncludes = !nostdinc;
775}
776
Daniel Dunbar999215c2009-11-11 06:10:03 +0000777void clang::InitializePreprocessorOptions(PreprocessorOptions &Opts) {
778 using namespace preprocessoroptions;
779
780 Opts.setImplicitPCHInclude(ImplicitIncludePCH);
781 Opts.setImplicitPTHInclude(ImplicitIncludePTH);
782
Daniel Dunbar29403032009-11-12 02:53:59 +0000783 // Select the token cache file, we don't support more than one currently so we
784 // can't have both an implicit-pth and a token cache file.
785 if (TokenCache.getPosition() && ImplicitIncludePTH.getPosition()) {
786 // FIXME: Don't fail like this.
787 fprintf(stderr, "error: cannot use both -token-cache and -include-pth "
788 "options\n");
789 exit(1);
790 }
791 if (TokenCache.getPosition())
792 Opts.setTokenCache(TokenCache);
793 else
794 Opts.setTokenCache(ImplicitIncludePTH);
795
Daniel Dunbar999215c2009-11-11 06:10:03 +0000796 // Use predefines?
797 Opts.setUsePredefines(!UndefMacros);
798
799 // Add macros from the command line.
800 unsigned d = 0, D = D_macros.size();
801 unsigned u = 0, U = U_macros.size();
802 while (d < D || u < U) {
803 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
804 Opts.addMacroDef(D_macros[d++]);
805 else
806 Opts.addMacroUndef(U_macros[u++]);
807 }
808
809 // If -imacros are specified, include them now. These are processed before
810 // any -include directives.
811 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
812 Opts.addMacroInclude(ImplicitMacroIncludes[i]);
813
814 // Add the ordered list of -includes, sorting in the implicit include options
815 // at the appropriate location.
816 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
817 std::string OriginalFile;
818
819 if (!ImplicitIncludePTH.empty())
820 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
821 &ImplicitIncludePTH));
822 if (!ImplicitIncludePCH.empty()) {
823 OriginalFile = PCHReader::getOriginalSourceFile(ImplicitIncludePCH);
824 // FIXME: Don't fail like this.
825 if (OriginalFile.empty())
826 exit(1);
827 OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
828 &OriginalFile));
829 }
830 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
831 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
832 &ImplicitIncludes[i]));
833 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
834
835 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
836 Opts.addInclude(*OrderedPaths[i].second);
837}
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000838
839void clang::InitializeLangOptions(LangOptions &Options, LangKind LK,
840 TargetInfo &Target,
Daniel Dunbar979586e2009-11-11 09:38:56 +0000841 const CompileOptions &CompileOpts) {
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000842 using namespace langoptions;
843
844 bool NoPreprocess = false;
845
846 switch (LK) {
847 default: assert(0 && "Unknown language kind!");
848 case langkind_asm_cpp:
849 Options.AsmPreprocessor = 1;
850 // FALLTHROUGH
851 case langkind_c_cpp:
852 NoPreprocess = true;
853 // FALLTHROUGH
854 case langkind_c:
855 // Do nothing.
856 break;
857 case langkind_cxx_cpp:
858 NoPreprocess = true;
859 // FALLTHROUGH
860 case langkind_cxx:
861 Options.CPlusPlus = 1;
862 break;
863 case langkind_objc_cpp:
864 NoPreprocess = true;
865 // FALLTHROUGH
866 case langkind_objc:
867 Options.ObjC1 = Options.ObjC2 = 1;
868 break;
869 case langkind_objcxx_cpp:
870 NoPreprocess = true;
871 // FALLTHROUGH
872 case langkind_objcxx:
873 Options.ObjC1 = Options.ObjC2 = 1;
874 Options.CPlusPlus = 1;
875 break;
876 case langkind_ocl:
877 Options.OpenCL = 1;
878 Options.AltiVec = 1;
879 Options.CXXOperatorNames = 1;
880 Options.LaxVectorConversions = 1;
881 break;
882 }
883
884 if (ObjCExclusiveGC)
885 Options.setGCMode(LangOptions::GCOnly);
886 else if (ObjCEnableGC)
887 Options.setGCMode(LangOptions::HybridGC);
888
889 if (ObjCEnableGCBitmapPrint)
890 Options.ObjCGCBitmapPrint = 1;
891
892 if (AltiVec)
893 Options.AltiVec = 1;
894
895 if (PThread)
896 Options.POSIXThreads = 1;
897
898 Options.setVisibilityMode(SymbolVisibility);
899 Options.OverflowChecking = OverflowChecking;
900
901
902 // Allow the target to set the default the language options as it sees fit.
903 Target.getDefaultLangOptions(Options);
904
905 // Pass the map of target features to the target for validation and
906 // processing.
Daniel Dunbar979586e2009-11-11 09:38:56 +0000907 Target.HandleTargetFeatures(CompileOpts.Features);
Daniel Dunbar84dfbfd2009-11-11 07:26:12 +0000908
909 if (LangStd == lang_unspecified) {
910 // Based on the base language, pick one.
911 switch (LK) {
912 case langkind_ast: assert(0 && "Invalid call for AST inputs");
913 case lang_unspecified: assert(0 && "Unknown base language");
914 case langkind_ocl:
915 LangStd = lang_c99;
916 break;
917 case langkind_c:
918 case langkind_asm_cpp:
919 case langkind_c_cpp:
920 case langkind_objc:
921 case langkind_objc_cpp:
922 LangStd = lang_gnu99;
923 break;
924 case langkind_cxx:
925 case langkind_cxx_cpp:
926 case langkind_objcxx:
927 case langkind_objcxx_cpp:
928 LangStd = lang_gnucxx98;
929 break;
930 }
931 }
932
933 switch (LangStd) {
934 default: assert(0 && "Unknown language standard!");
935
936 // Fall through from newer standards to older ones. This isn't really right.
937 // FIXME: Enable specifically the right features based on the language stds.
938 case lang_gnucxx0x:
939 case lang_cxx0x:
940 Options.CPlusPlus0x = 1;
941 // FALL THROUGH
942 case lang_gnucxx98:
943 case lang_cxx98:
944 Options.CPlusPlus = 1;
945 Options.CXXOperatorNames = !NoOperatorNames;
946 // FALL THROUGH.
947 case lang_gnu99:
948 case lang_c99:
949 Options.C99 = 1;
950 Options.HexFloats = 1;
951 // FALL THROUGH.
952 case lang_gnu89:
953 Options.BCPLComment = 1; // Only for C99/C++.
954 // FALL THROUGH.
955 case lang_c94:
956 Options.Digraphs = 1; // C94, C99, C++.
957 // FALL THROUGH.
958 case lang_c89:
959 break;
960 }
961
962 // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc.
963 switch (LangStd) {
964 default: assert(0 && "Unknown language standard!");
965 case lang_gnucxx0x:
966 case lang_gnucxx98:
967 case lang_gnu99:
968 case lang_gnu89:
969 Options.GNUMode = 1;
970 break;
971 case lang_cxx0x:
972 case lang_cxx98:
973 case lang_c99:
974 case lang_c94:
975 case lang_c89:
976 Options.GNUMode = 0;
977 break;
978 }
979
980 if (Options.CPlusPlus) {
981 Options.C99 = 0;
982 Options.HexFloats = 0;
983 }
984
985 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
986 Options.ImplicitInt = 1;
987 else
988 Options.ImplicitInt = 0;
989
990 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
991 // is specified, or -std is set to a conforming mode.
992 Options.Trigraphs = !Options.GNUMode;
993 if (Trigraphs.getPosition())
994 Options.Trigraphs = Trigraphs; // Command line option wins if specified.
995
996 // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off
997 // even if they are normally on for the target. In GNU modes (e.g.
998 // -std=gnu99) the default for blocks depends on the target settings.
999 // However, blocks are not turned off when compiling Obj-C or Obj-C++ code.
1000 if (!Options.ObjC1 && !Options.GNUMode)
1001 Options.Blocks = 0;
1002
1003 // Default to not accepting '$' in identifiers when preprocessing assembler,
1004 // but do accept when preprocessing C. FIXME: these defaults are right for
1005 // darwin, are they right everywhere?
1006 Options.DollarIdents = LK != langkind_asm_cpp;
1007 if (DollarsInIdents.getPosition()) // Explicit setting overrides default.
1008 Options.DollarIdents = DollarsInIdents;
1009
1010 if (PascalStrings.getPosition())
1011 Options.PascalStrings = PascalStrings;
1012 if (MSExtensions.getPosition())
1013 Options.Microsoft = MSExtensions;
1014 Options.WritableStrings = WritableStrings;
1015 if (NoLaxVectorConversions.getPosition())
1016 Options.LaxVectorConversions = 0;
1017 Options.Exceptions = Exceptions;
1018 Options.Rtti = Rtti;
1019 if (EnableBlocks.getPosition())
1020 Options.Blocks = EnableBlocks;
1021 if (CharIsSigned.getPosition())
1022 Options.CharIsSigned = CharIsSigned;
1023 if (ShortWChar.getPosition())
1024 Options.ShortWChar = ShortWChar;
1025
1026 if (!AllowBuiltins)
1027 Options.NoBuiltin = 1;
1028 if (Freestanding)
1029 Options.Freestanding = Options.NoBuiltin = 1;
1030
1031 if (EnableHeinousExtensions)
1032 Options.HeinousExtensions = 1;
1033
1034 if (AccessControl)
1035 Options.AccessControl = 1;
1036
1037 Options.ElideConstructors = !NoElideConstructors;
1038
1039 // OpenCL and C++ both have bool, true, false keywords.
1040 Options.Bool = Options.OpenCL | Options.CPlusPlus;
1041
1042 Options.MathErrno = MathErrno;
1043
1044 Options.InstantiationDepth = TemplateDepth;
1045
1046 // Override the default runtime if the user requested it.
1047 if (NeXTRuntime)
1048 Options.NeXTRuntime = 1;
1049 else if (GNURuntime)
1050 Options.NeXTRuntime = 0;
1051
1052 if (!ObjCConstantStringClass.empty())
1053 Options.ObjCConstantStringClass = ObjCConstantStringClass.c_str();
1054
1055 if (ObjCNonFragileABI)
1056 Options.ObjCNonFragileABI = 1;
1057
1058 if (EmitAllDecls)
1059 Options.EmitAllDecls = 1;
1060
1061 // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't support.
1062 Options.OptimizeSize = 0;
1063 Options.Optimize = !!CompileOpts.OptimizationLevel;
1064
1065 assert(PICLevel <= 2 && "Invalid value for -pic-level");
1066 Options.PICLevel = PICLevel;
1067
1068 Options.GNUInline = !Options.C99;
1069 // FIXME: This is affected by other options (-fno-inline).
1070
1071 // This is the __NO_INLINE__ define, which just depends on things like the
1072 // optimization level and -fno-inline, not actually whether the backend has
1073 // inlining enabled.
1074 Options.NoInline = !CompileOpts.OptimizationLevel;
1075
1076 Options.Static = StaticDefine;
1077
1078 switch (StackProtector) {
1079 default:
1080 assert(StackProtector <= 2 && "Invalid value for -stack-protector");
1081 case -1: break;
1082 case 0: Options.setStackProtectorMode(LangOptions::SSPOff); break;
1083 case 1: Options.setStackProtectorMode(LangOptions::SSPOn); break;
1084 case 2: Options.setStackProtectorMode(LangOptions::SSPReq); break;
1085 }
1086
1087 if (MainFileName.getPosition())
1088 Options.setMainFileName(MainFileName.c_str());
1089
1090 Target.setForcedLangOptions(Options);
1091}
Daniel Dunbar22bdabf2009-11-11 10:07:44 +00001092
1093void
1094clang::InitializePreprocessorOutputOptions(PreprocessorOutputOptions &Opts) {
1095 using namespace preprocessoroutputoptions;
1096
1097 Opts.ShowCPP = !DumpMacros;
1098 Opts.ShowMacros = DumpMacros || DumpDefines;
1099 Opts.ShowLineMarkers = !DisableLineMarkers;
1100 Opts.ShowComments = EnableCommentOutput;
1101 Opts.ShowMacroComments = EnableMacroCommentOutput;
1102}
1103