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