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