blob: 33b8d77d15832914d97c03d2a7f05bce3ab3772a [file] [log] [blame]
Daniel Dunbar0498cfc2009-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 Dunbar339c1342009-11-11 08:13:55 +000015#include "clang/Basic/LangOptions.h"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Frontend/AnalysisConsumer.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000018#include "clang/CodeGen/CodeGenOptions.h"
Daniel Dunbar0e0bae82009-11-11 21:43:12 +000019#include "clang/Frontend/DependencyOutputOptions.h"
Daniel Dunbar0db4b762009-11-11 08:13:40 +000020#include "clang/Frontend/DiagnosticOptions.h"
Daniel Dunbar26266882009-11-12 23:52:32 +000021#include "clang/Frontend/FrontendOptions.h"
Daniel Dunbarf7973292009-11-11 08:13:32 +000022#include "clang/Frontend/HeaderSearchOptions.h"
Daniel Dunbarb52d2432009-11-11 06:10:03 +000023#include "clang/Frontend/PCHReader.h"
24#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbar29cf7462009-11-11 10:07:44 +000025#include "clang/Frontend/PreprocessorOutputOptions.h"
Daniel Dunbarb52d2432009-11-11 06:10:03 +000026#include "llvm/ADT/STLExtras.h"
Daniel Dunbar0498cfc2009-11-10 19:51:53 +000027#include "llvm/ADT/StringMap.h"
28#include "llvm/Support/CommandLine.h"
Dan Gohman0063e982009-11-10 21:21:27 +000029#include <stdio.h>
Daniel Dunbar0498cfc2009-11-10 19:51:53 +000030
31using namespace clang;
32
33//===----------------------------------------------------------------------===//
Daniel Dunbar339c1342009-11-11 08:13:55 +000034// Analyzer Options
35//===----------------------------------------------------------------------===//
36
37namespace analyzeroptions {
38
39static llvm::cl::list<Analyses>
40AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"),
41llvm::cl::values(
42#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
43clEnumValN(NAME, CMDFLAG, DESC),
44#include "clang/Frontend/Analyses.def"
45clEnumValEnd));
46
47static llvm::cl::opt<AnalysisStores>
48AnalysisStoreOpt("analyzer-store",
49 llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"),
50 llvm::cl::init(BasicStoreModel),
51 llvm::cl::values(
52#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\
53clEnumValN(NAME##Model, CMDFLAG, DESC),
54#include "clang/Frontend/Analyses.def"
55clEnumValEnd));
56
57static llvm::cl::opt<AnalysisConstraints>
58AnalysisConstraintsOpt("analyzer-constraints",
59 llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"),
60 llvm::cl::init(RangeConstraintsModel),
61 llvm::cl::values(
62#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\
63clEnumValN(NAME##Model, CMDFLAG, DESC),
64#include "clang/Frontend/Analyses.def"
65clEnumValEnd));
66
67static llvm::cl::opt<AnalysisDiagClients>
68AnalysisDiagOpt("analyzer-output",
69 llvm::cl::desc("Source Code Analysis - Output Options"),
70 llvm::cl::init(PD_HTML),
71 llvm::cl::values(
72#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\
73clEnumValN(PD_##NAME, CMDFLAG, DESC),
74#include "clang/Frontend/Analyses.def"
75clEnumValEnd));
76
77static llvm::cl::opt<bool>
78AnalyzeAll("analyzer-opt-analyze-headers",
79 llvm::cl::desc("Force the static analyzer to analyze "
80 "functions defined in header files"));
Ted Kremenekeb941132009-11-13 01:15:47 +000081
Daniel Dunbar339c1342009-11-11 08:13:55 +000082static llvm::cl::opt<bool>
83AnalyzerDisplayProgress("analyzer-display-progress",
Ted Kremenekeb941132009-11-13 01:15:47 +000084 llvm::cl::desc("Emit verbose output about the analyzer's progress"));
Daniel Dunbar339c1342009-11-11 08:13:55 +000085
Ted Kremenekeb941132009-11-13 01:15:47 +000086static llvm::cl::opt<bool>
87AnalyzerExperimentalChecks("analyzer-experimental-checks",
88 llvm::cl::desc("Use experimental path-sensitive checks"));
Ted Kremenek8382cf52009-11-13 18:46:29 +000089
90static llvm::cl::opt<bool>
91AnalyzerExperimentalInternalChecks("analyzer-experimental-internal-checks",
92 llvm::cl::desc("Use new default path-sensitive checks currently in testing"));
Ted Kremenekeb941132009-11-13 01:15:47 +000093
Daniel Dunbar339c1342009-11-11 08:13:55 +000094static llvm::cl::opt<std::string>
95AnalyzeSpecificFunction("analyze-function",
96 llvm::cl::desc("Run analysis on specific function"));
97
98static llvm::cl::opt<bool>
99EagerlyAssume("analyzer-eagerly-assume",
100 llvm::cl::init(false),
101 llvm::cl::desc("Eagerly assume the truth/falseness of some "
Ted Kremenekeb941132009-11-13 01:15:47 +0000102 "symbolic constraints"));
Daniel Dunbar339c1342009-11-11 08:13:55 +0000103
104static llvm::cl::opt<bool>
105PurgeDead("analyzer-purge-dead",
106 llvm::cl::init(true),
107 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
Ted Kremenekeb941132009-11-13 01:15:47 +0000108 " processing a statement"));
Daniel Dunbar339c1342009-11-11 08:13:55 +0000109
110static llvm::cl::opt<bool>
111TrimGraph("trim-egraph",
112 llvm::cl::desc("Only show error-related paths in the analysis graph"));
113
114static llvm::cl::opt<bool>
115VisualizeEGDot("analyzer-viz-egraph-graphviz",
116 llvm::cl::desc("Display exploded graph using GraphViz"));
117
118static llvm::cl::opt<bool>
119VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
120 llvm::cl::desc("Display exploded graph using Ubigraph"));
121
122}
123
124void clang::InitializeAnalyzerOptions(AnalyzerOptions &Opts) {
125 using namespace analyzeroptions;
126 Opts.AnalysisList = AnalysisList;
127 Opts.AnalysisStoreOpt = AnalysisStoreOpt;
128 Opts.AnalysisConstraintsOpt = AnalysisConstraintsOpt;
129 Opts.AnalysisDiagOpt = AnalysisDiagOpt;
130 Opts.VisualizeEGDot = VisualizeEGDot;
131 Opts.VisualizeEGUbi = VisualizeEGUbi;
132 Opts.AnalyzeAll = AnalyzeAll;
133 Opts.AnalyzerDisplayProgress = AnalyzerDisplayProgress;
134 Opts.PurgeDead = PurgeDead;
135 Opts.EagerlyAssume = EagerlyAssume;
136 Opts.AnalyzeSpecificFunction = AnalyzeSpecificFunction;
Ted Kremenekeb941132009-11-13 01:15:47 +0000137 Opts.EnableExperimentalChecks = AnalyzerExperimentalChecks;
Ted Kremenek8382cf52009-11-13 18:46:29 +0000138 Opts.EnableExperimentalInternalChecks = AnalyzerExperimentalInternalChecks;
Daniel Dunbar339c1342009-11-11 08:13:55 +0000139 Opts.TrimGraph = TrimGraph;
140}
141
142
143//===----------------------------------------------------------------------===//
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000144// Code Generation Options
145//===----------------------------------------------------------------------===//
146
147namespace codegenoptions {
148
149static llvm::cl::opt<bool>
150DisableLLVMOptimizations("disable-llvm-optzns",
151 llvm::cl::desc("Don't run LLVM optimization passes"));
152
153static llvm::cl::opt<bool>
154DisableRedZone("disable-red-zone",
155 llvm::cl::desc("Do not emit code that uses the red zone."),
156 llvm::cl::init(false));
157
158static llvm::cl::opt<bool>
159GenerateDebugInfo("g",
160 llvm::cl::desc("Generate source level debug information"));
161
162static llvm::cl::opt<bool>
163NoCommon("fno-common",
164 llvm::cl::desc("Compile common globals like normal definitions"),
165 llvm::cl::ValueDisallowed);
166
167static llvm::cl::opt<bool>
168NoImplicitFloat("no-implicit-float",
169 llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"),
170 llvm::cl::init(false));
171
172static llvm::cl::opt<bool>
173NoMergeConstants("fno-merge-all-constants",
174 llvm::cl::desc("Disallow merging of constants."));
175
176// It might be nice to add bounds to the CommandLine library directly.
177struct OptLevelParser : public llvm::cl::parser<unsigned> {
178 bool parse(llvm::cl::Option &O, llvm::StringRef ArgName,
179 llvm::StringRef Arg, unsigned &Val) {
180 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
181 return true;
182 if (Val > 3)
183 return O.error("'" + Arg + "' invalid optimization level!");
184 return false;
185 }
186};
187static llvm::cl::opt<unsigned, false, OptLevelParser>
188OptLevel("O", llvm::cl::Prefix,
189 llvm::cl::desc("Optimization level"),
190 llvm::cl::init(0));
191
192static llvm::cl::opt<bool>
193OptSize("Os", llvm::cl::desc("Optimize for size"));
194
195static llvm::cl::opt<std::string>
196TargetCPU("mcpu",
197 llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
198
199static llvm::cl::list<std::string>
200TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes"));
201
202}
203
204//===----------------------------------------------------------------------===//
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000205// Dependency Output Options
206//===----------------------------------------------------------------------===//
207
208namespace dependencyoutputoptions {
209
210static llvm::cl::opt<std::string>
211DependencyFile("dependency-file",
212 llvm::cl::desc("Filename (or -) to write dependency output to"));
213
214static llvm::cl::opt<bool>
215DependenciesIncludeSystemHeaders("sys-header-deps",
216 llvm::cl::desc("Include system headers in dependency output"));
217
218static llvm::cl::list<std::string>
219DependencyTargets("MT",
220 llvm::cl::desc("Specify target for dependency"));
221
222static llvm::cl::opt<bool>
223PhonyDependencyTarget("MP",
224 llvm::cl::desc("Create phony target for each dependency "
225 "(other than main file)"));
226
227}
228
229//===----------------------------------------------------------------------===//
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000230// Diagnostic Options
231//===----------------------------------------------------------------------===//
232
233namespace diagnosticoptions {
234
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000235static llvm::cl::opt<std::string>
236DumpBuildInformation("dump-build-information",
237 llvm::cl::value_desc("filename"),
238 llvm::cl::desc("output a dump of some build information to a file"));
239
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000240static llvm::cl::opt<bool>
241NoShowColumn("fno-show-column",
242 llvm::cl::desc("Do not include column number on diagnostics"));
243
244static llvm::cl::opt<bool>
245NoShowLocation("fno-show-source-location",
246 llvm::cl::desc("Do not include source location information with"
247 " diagnostics"));
248
249static llvm::cl::opt<bool>
250NoCaretDiagnostics("fno-caret-diagnostics",
251 llvm::cl::desc("Do not include source line and caret with"
252 " diagnostics"));
253
254static llvm::cl::opt<bool>
255NoDiagnosticsFixIt("fno-diagnostics-fixit-info",
256 llvm::cl::desc("Do not include fixit information in"
257 " diagnostics"));
258
Daniel Dunbar69079432009-11-12 07:28:44 +0000259static llvm::cl::opt<bool> OptNoWarnings("w");
260
261static llvm::cl::opt<bool> OptPedantic("pedantic");
262
263static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
264
265// This gets all -W options, including -Werror, -W[no-]system-headers, etc. The
266// driver has stripped off -Wa,foo etc. The driver has also translated -W to
267// -Wextra, so we don't need to worry about it.
268static llvm::cl::list<std::string>
269OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional);
270
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000271static llvm::cl::opt<bool>
272PrintSourceRangeInfo("fdiagnostics-print-source-range-info",
273 llvm::cl::desc("Print source range spans in numeric form"));
274
275static llvm::cl::opt<bool>
276PrintDiagnosticOption("fdiagnostics-show-option",
277 llvm::cl::desc("Print diagnostic name with mappable diagnostics"));
278
279static llvm::cl::opt<unsigned>
280MessageLength("fmessage-length",
281 llvm::cl::desc("Format message diagnostics so that they fit "
282 "within N columns or fewer, when possible."),
283 llvm::cl::value_desc("N"));
284
285static llvm::cl::opt<bool>
286PrintColorDiagnostic("fcolor-diagnostics",
287 llvm::cl::desc("Use colors in diagnostics"));
288
Daniel Dunbar69079432009-11-12 07:28:44 +0000289static llvm::cl::opt<bool>
290SilenceRewriteMacroWarning("Wno-rewrite-macros", llvm::cl::init(false),
291 llvm::cl::desc("Silence ObjC rewriting warnings"));
292
Daniel Dunbar26266882009-11-12 23:52:32 +0000293static llvm::cl::opt<bool>
294VerifyDiagnostics("verify",
295 llvm::cl::desc("Verify emitted diagnostics and warnings"));
296
297}
298
299
300//===----------------------------------------------------------------------===//
301// Frontend Options
302//===----------------------------------------------------------------------===//
303
304namespace frontendoptions {
305
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000306using namespace clang::frontend;
307
Daniel Dunbar914474c2009-11-13 01:02:10 +0000308static llvm::cl::opt<ParsedSourceLocation>
309CodeCompletionAt("code-completion-at",
310 llvm::cl::value_desc("file:line:column"),
311 llvm::cl::desc("Dump code-completion information at a location"));
312
313static llvm::cl::opt<bool>
314CodeCompletionDebugPrinter("code-completion-debug-printer",
315 llvm::cl::desc("Use the \"debug\" code-completion print"),
316 llvm::cl::init(true));
317
318static llvm::cl::opt<bool>
319CodeCompletionWantsMacros("code-completion-macros",
320 llvm::cl::desc("Include macros in code-completion results"));
321
Daniel Dunbar26266882009-11-12 23:52:32 +0000322static llvm::cl::opt<bool>
323DisableFree("disable-free",
324 llvm::cl::desc("Disable freeing of memory on exit"),
325 llvm::cl::init(false));
326
327static llvm::cl::opt<bool>
328EmptyInputOnly("empty-input-only",
329 llvm::cl::desc("Force running on an empty input file"));
330
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000331static llvm::cl::opt<FrontendOptions::InputKind>
332InputType("x", llvm::cl::desc("Input language type"),
333 llvm::cl::init(FrontendOptions::IK_None),
334 llvm::cl::values(clEnumValN(FrontendOptions::IK_C, "c", "C"),
335 clEnumValN(FrontendOptions::IK_OpenCL, "cl", "OpenCL C"),
336 clEnumValN(FrontendOptions::IK_CXX, "c++", "C++"),
337 clEnumValN(FrontendOptions::IK_ObjC, "objective-c",
338 "Objective C"),
339 clEnumValN(FrontendOptions::IK_ObjCXX, "objective-c++",
340 "Objective C++"),
341 clEnumValN(FrontendOptions::IK_PreprocessedC,
342 "cpp-output",
343 "Preprocessed C"),
344 clEnumValN(FrontendOptions::IK_Asm,
345 "assembler-with-cpp",
346 "Assembly Source Codde"),
347 clEnumValN(FrontendOptions::IK_PreprocessedCXX,
348 "c++-cpp-output",
349 "Preprocessed C++"),
350 clEnumValN(FrontendOptions::IK_PreprocessedObjC,
351 "objective-c-cpp-output",
352 "Preprocessed Objective C"),
353 clEnumValN(FrontendOptions::IK_PreprocessedObjCXX,
354 "objective-c++-cpp-output",
355 "Preprocessed Objective C++"),
356 clEnumValN(FrontendOptions::IK_C, "c-header",
357 "C header"),
358 clEnumValN(FrontendOptions::IK_ObjC, "objective-c-header",
359 "Objective-C header"),
360 clEnumValN(FrontendOptions::IK_CXX, "c++-header",
361 "C++ header"),
362 clEnumValN(FrontendOptions::IK_ObjCXX,
363 "objective-c++-header",
364 "Objective-C++ header"),
365 clEnumValN(FrontendOptions::IK_AST, "ast",
366 "Clang AST"),
367 clEnumValEnd));
368
Daniel Dunbar26266882009-11-12 23:52:32 +0000369static llvm::cl::list<std::string>
370InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
371
372static llvm::cl::opt<std::string>
373InheritanceViewCls("cxx-inheritance-view",
374 llvm::cl::value_desc("class name"),
375 llvm::cl::desc("View C++ inheritance for a specified class"));
376
Daniel Dunbarc86804b2009-11-12 23:52:56 +0000377static llvm::cl::list<ParsedSourceLocation>
378FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"),
379 llvm::cl::desc("Perform Fix-It modifications at the given source location"));
380
Daniel Dunbar26266882009-11-12 23:52:32 +0000381static llvm::cl::opt<std::string>
382OutputFile("o",
383 llvm::cl::value_desc("path"),
384 llvm::cl::desc("Specify output file"));
385
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000386static llvm::cl::opt<ActionKind>
387ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
388 llvm::cl::init(ParseSyntaxOnly),
389 llvm::cl::values(
390 clEnumValN(RunPreprocessorOnly, "Eonly",
391 "Just run preprocessor, no output (for timings)"),
392 clEnumValN(PrintPreprocessedInput, "E",
393 "Run preprocessor, emit preprocessed file"),
394 clEnumValN(DumpRawTokens, "dump-raw-tokens",
395 "Lex file in raw mode and dump raw tokens"),
396 clEnumValN(RunAnalysis, "analyze",
397 "Run static analysis engine"),
398 clEnumValN(DumpTokens, "dump-tokens",
399 "Run preprocessor, dump internal rep of tokens"),
400 clEnumValN(ParseNoop, "parse-noop",
401 "Run parser with noop callbacks (for timings)"),
402 clEnumValN(ParseSyntaxOnly, "fsyntax-only",
403 "Run parser and perform semantic analysis"),
404 clEnumValN(FixIt, "fixit",
405 "Apply fix-it advice to the input source"),
406 clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
407 "Run parser and print each callback invoked"),
408 clEnumValN(EmitHTML, "emit-html",
409 "Output input source as HTML"),
410 clEnumValN(ASTPrint, "ast-print",
411 "Build ASTs and then pretty-print them"),
412 clEnumValN(ASTPrintXML, "ast-print-xml",
413 "Build ASTs and then print them in XML format"),
414 clEnumValN(ASTDump, "ast-dump",
415 "Build ASTs and then debug dump them"),
416 clEnumValN(ASTView, "ast-view",
417 "Build ASTs and view them with GraphViz"),
418 clEnumValN(PrintDeclContext, "print-decl-contexts",
419 "Print DeclContexts and their Decls"),
420 clEnumValN(DumpRecordLayouts, "dump-record-layouts",
421 "Dump record layout information"),
422 clEnumValN(GeneratePTH, "emit-pth",
423 "Generate pre-tokenized header file"),
424 clEnumValN(GeneratePCH, "emit-pch",
425 "Generate pre-compiled header file"),
426 clEnumValN(EmitAssembly, "S",
427 "Emit native assembly code"),
428 clEnumValN(EmitLLVM, "emit-llvm",
429 "Build ASTs then convert to LLVM, emit .ll file"),
430 clEnumValN(EmitBC, "emit-llvm-bc",
431 "Build ASTs then convert to LLVM, emit .bc file"),
432 clEnumValN(EmitLLVMOnly, "emit-llvm-only",
433 "Build ASTs and convert to LLVM, discarding output"),
434 clEnumValN(RewriteTest, "rewrite-test",
435 "Rewriter playground"),
436 clEnumValN(RewriteObjC, "rewrite-objc",
437 "Rewrite ObjC into C (code rewriter example)"),
438 clEnumValN(RewriteMacros, "rewrite-macros",
439 "Expand macros without full preprocessing"),
440 clEnumValN(RewriteBlocks, "rewrite-blocks",
441 "Rewrite Blocks to C"),
442 clEnumValEnd));
443
Daniel Dunbar26266882009-11-12 23:52:32 +0000444static llvm::cl::opt<bool>
445RelocatablePCH("relocatable-pch",
446 llvm::cl::desc("Whether to build a relocatable precompiled "
447 "header"));
448static llvm::cl::opt<bool>
449Stats("print-stats",
450 llvm::cl::desc("Print performance metrics and statistics"));
451
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000452static llvm::cl::opt<std::string>
453TargetABI("target-abi",
454 llvm::cl::desc("Target a particular ABI type"));
455
456static llvm::cl::opt<std::string>
457TargetTriple("triple",
458 llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)"));
459
Daniel Dunbar26266882009-11-12 23:52:32 +0000460static llvm::cl::opt<bool>
461TimeReport("ftime-report",
462 llvm::cl::desc("Print the amount of time each "
463 "phase of compilation takes"));
464
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000465}
466
467//===----------------------------------------------------------------------===//
Daniel Dunbar56749082009-11-11 07:26:12 +0000468// Language Options
469//===----------------------------------------------------------------------===//
470
471namespace langoptions {
472
473static llvm::cl::opt<bool>
474AllowBuiltins("fbuiltin", llvm::cl::init(true),
475 llvm::cl::desc("Disable implicit builtin knowledge of functions"));
476
477static llvm::cl::opt<bool>
478AltiVec("faltivec", llvm::cl::desc("Enable AltiVec vector initializer syntax"),
479 llvm::cl::init(false));
480
481static llvm::cl::opt<bool>
482AccessControl("faccess-control",
483 llvm::cl::desc("Enable C++ access control"));
484
485static llvm::cl::opt<bool>
486CharIsSigned("fsigned-char",
487 llvm::cl::desc("Force char to be a signed/unsigned type"));
488
489static llvm::cl::opt<bool>
490DollarsInIdents("fdollars-in-identifiers",
491 llvm::cl::desc("Allow '$' in identifiers"));
492
493static llvm::cl::opt<bool>
494EmitAllDecls("femit-all-decls",
495 llvm::cl::desc("Emit all declarations, even if unused"));
496
497static llvm::cl::opt<bool>
498EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"));
499
500static llvm::cl::opt<bool>
501EnableHeinousExtensions("fheinous-gnu-extensions",
502 llvm::cl::desc("enable GNU extensions that you really really shouldn't use"),
503 llvm::cl::ValueDisallowed, llvm::cl::Hidden);
504
505static llvm::cl::opt<bool>
506Exceptions("fexceptions",
507 llvm::cl::desc("Enable support for exception handling"));
508
509static llvm::cl::opt<bool>
510Freestanding("ffreestanding",
511 llvm::cl::desc("Assert that the compilation takes place in a "
512 "freestanding environment"));
513
514static llvm::cl::opt<bool>
515GNURuntime("fgnu-runtime",
516 llvm::cl::desc("Generate output compatible with the standard GNU "
517 "Objective-C runtime"));
518
519/// LangStds - Language standards we support.
520enum LangStds {
521 lang_unspecified,
522 lang_c89, lang_c94, lang_c99,
523 lang_gnu89, lang_gnu99,
524 lang_cxx98, lang_gnucxx98,
525 lang_cxx0x, lang_gnucxx0x
526};
527static llvm::cl::opt<LangStds>
528LangStd("std", llvm::cl::desc("Language standard to compile for"),
529 llvm::cl::init(lang_unspecified),
530 llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
531 clEnumValN(lang_c89, "c90", "ISO C 1990"),
532 clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
533 clEnumValN(lang_c94, "iso9899:199409",
534 "ISO C 1990 with amendment 1"),
535 clEnumValN(lang_c99, "c99", "ISO C 1999"),
536 clEnumValN(lang_c99, "c9x", "ISO C 1999"),
537 clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
538 clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
539 clEnumValN(lang_gnu89, "gnu89",
540 "ISO C 1990 with GNU extensions"),
541 clEnumValN(lang_gnu99, "gnu99",
542 "ISO C 1999 with GNU extensions (default for C)"),
543 clEnumValN(lang_gnu99, "gnu9x",
544 "ISO C 1999 with GNU extensions"),
545 clEnumValN(lang_cxx98, "c++98",
546 "ISO C++ 1998 with amendments"),
547 clEnumValN(lang_gnucxx98, "gnu++98",
548 "ISO C++ 1998 with amendments and GNU "
549 "extensions (default for C++)"),
550 clEnumValN(lang_cxx0x, "c++0x",
551 "Upcoming ISO C++ 200x with amendments"),
552 clEnumValN(lang_gnucxx0x, "gnu++0x",
553 "Upcoming ISO C++ 200x with amendments and GNU "
554 "extensions"),
555 clEnumValEnd));
556
557static llvm::cl::opt<bool>
558MSExtensions("fms-extensions",
559 llvm::cl::desc("Accept some non-standard constructs used in "
560 "Microsoft header files "));
561
562static llvm::cl::opt<std::string>
563MainFileName("main-file-name",
564 llvm::cl::desc("Main file name to use for debug info"));
565
566static llvm::cl::opt<bool>
567MathErrno("fmath-errno", llvm::cl::init(true),
568 llvm::cl::desc("Require math functions to respect errno"));
569
570static llvm::cl::opt<bool>
571NeXTRuntime("fnext-runtime",
572 llvm::cl::desc("Generate output compatible with the NeXT "
573 "runtime"));
574
575static llvm::cl::opt<bool>
576NoElideConstructors("fno-elide-constructors",
577 llvm::cl::desc("Disable C++ copy constructor elision"));
578
579static llvm::cl::opt<bool>
580NoLaxVectorConversions("fno-lax-vector-conversions",
581 llvm::cl::desc("Disallow implicit conversions between "
582 "vectors with a different number of "
583 "elements or different element types"));
584
585
586static llvm::cl::opt<bool>
587NoOperatorNames("fno-operator-names",
588 llvm::cl::desc("Do not treat C++ operator name keywords as "
589 "synonyms for operators"));
590
591static llvm::cl::opt<std::string>
592ObjCConstantStringClass("fconstant-string-class",
593 llvm::cl::value_desc("class name"),
594 llvm::cl::desc("Specify the class to use for constant "
595 "Objective-C string objects."));
596
597static llvm::cl::opt<bool>
598ObjCEnableGC("fobjc-gc",
599 llvm::cl::desc("Enable Objective-C garbage collection"));
600
601static llvm::cl::opt<bool>
602ObjCExclusiveGC("fobjc-gc-only",
603 llvm::cl::desc("Use GC exclusively for Objective-C related "
604 "memory management"));
605
606static llvm::cl::opt<bool>
607ObjCEnableGCBitmapPrint("print-ivar-layout",
608 llvm::cl::desc("Enable Objective-C Ivar layout bitmap print trace"));
609
610static llvm::cl::opt<bool>
611ObjCNonFragileABI("fobjc-nonfragile-abi",
612 llvm::cl::desc("enable objective-c's nonfragile abi"));
613
614static llvm::cl::opt<bool>
615OverflowChecking("ftrapv",
616 llvm::cl::desc("Trap on integer overflow"),
617 llvm::cl::init(false));
618
619static llvm::cl::opt<unsigned>
620PICLevel("pic-level", llvm::cl::desc("Value for __PIC__"));
621
622static llvm::cl::opt<bool>
623PThread("pthread", llvm::cl::desc("Support POSIX threads in generated code"),
624 llvm::cl::init(false));
625
626static llvm::cl::opt<bool>
627PascalStrings("fpascal-strings",
628 llvm::cl::desc("Recognize and construct Pascal-style "
629 "string literals"));
630
Daniel Dunbar56749082009-11-11 07:26:12 +0000631static llvm::cl::opt<bool>
632Rtti("frtti", llvm::cl::init(true),
633 llvm::cl::desc("Enable generation of rtti information"));
634
635static llvm::cl::opt<bool>
636ShortWChar("fshort-wchar",
637 llvm::cl::desc("Force wchar_t to be a short unsigned int"));
638
639static llvm::cl::opt<bool>
640StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined"));
641
642static llvm::cl::opt<int>
643StackProtector("stack-protector",
644 llvm::cl::desc("Enable stack protectors"),
645 llvm::cl::init(-1));
646
647static llvm::cl::opt<LangOptions::VisibilityMode>
648SymbolVisibility("fvisibility",
649 llvm::cl::desc("Set the default symbol visibility:"),
650 llvm::cl::init(LangOptions::Default),
651 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
652 "Use default symbol visibility"),
653 clEnumValN(LangOptions::Hidden, "hidden",
654 "Use hidden symbol visibility"),
655 clEnumValN(LangOptions::Protected,"protected",
656 "Use protected symbol visibility"),
657 clEnumValEnd));
658
659static llvm::cl::opt<unsigned>
660TemplateDepth("ftemplate-depth", llvm::cl::init(99),
661 llvm::cl::desc("Maximum depth of recursive template "
662 "instantiation"));
663
664static llvm::cl::opt<bool>
665Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences"));
666
667static llvm::cl::opt<bool>
668WritableStrings("fwritable-strings",
669 llvm::cl::desc("Store string literals as writable data"));
670
671}
672
673//===----------------------------------------------------------------------===//
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000674// General Preprocessor Options
675//===----------------------------------------------------------------------===//
676
677namespace preprocessoroptions {
678
679static llvm::cl::list<std::string>
680D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
681 llvm::cl::desc("Predefine the specified macro"));
682
683static llvm::cl::list<std::string>
684ImplicitIncludes("include", llvm::cl::value_desc("file"),
685 llvm::cl::desc("Include file before parsing"));
686static llvm::cl::list<std::string>
687ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"),
688 llvm::cl::desc("Include macros from file before parsing"));
689
690static llvm::cl::opt<std::string>
691ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"),
692 llvm::cl::desc("Include precompiled header file"));
693
694static llvm::cl::opt<std::string>
695ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"),
696 llvm::cl::desc("Include file before parsing"));
697
Daniel Dunbarb3cb98e2009-11-12 02:53:59 +0000698static llvm::cl::opt<std::string>
699TokenCache("token-cache", llvm::cl::value_desc("path"),
700 llvm::cl::desc("Use specified token cache file"));
701
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000702static llvm::cl::list<std::string>
703U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
704 llvm::cl::desc("Undefine the specified macro"));
705
706static llvm::cl::opt<bool>
707UndefMacros("undef", llvm::cl::value_desc("macro"),
708 llvm::cl::desc("undef all system defines"));
709
710}
711
712//===----------------------------------------------------------------------===//
Daniel Dunbarf7973292009-11-11 08:13:32 +0000713// Header Search Options
714//===----------------------------------------------------------------------===//
715
716namespace headersearchoptions {
717
718static llvm::cl::opt<bool>
719nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
720
721static llvm::cl::opt<bool>
722nobuiltininc("nobuiltininc",
723 llvm::cl::desc("Disable builtin #include directories"));
724
725// Various command line options. These four add directories to each chain.
726static llvm::cl::list<std::string>
727F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
728 llvm::cl::desc("Add directory to framework include search path"));
729
730static llvm::cl::list<std::string>
731I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
732 llvm::cl::desc("Add directory to include search path"));
733
734static llvm::cl::list<std::string>
735idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
736 llvm::cl::desc("Add directory to AFTER include search path"));
737
738static llvm::cl::list<std::string>
739iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
740 llvm::cl::desc("Add directory to QUOTE include search path"));
741
742static llvm::cl::list<std::string>
743isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
744 llvm::cl::desc("Add directory to SYSTEM include search path"));
745
746// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
747static llvm::cl::list<std::string>
748iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
749 llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
750static llvm::cl::list<std::string>
751iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
752 llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
753static llvm::cl::list<std::string>
754iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
755 llvm::cl::Prefix,
756 llvm::cl::desc("Set directory to include search path with prefix"));
757
758static llvm::cl::opt<std::string>
759isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
760 llvm::cl::desc("Set the system root directory (usually /)"));
761
Daniel Dunbar1417c742009-11-12 23:52:46 +0000762static llvm::cl::opt<bool>
763Verbose("v", llvm::cl::desc("Enable verbose output"));
764
Daniel Dunbarf7973292009-11-11 08:13:32 +0000765}
766
767//===----------------------------------------------------------------------===//
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000768// Preprocessed Output Options
769//===----------------------------------------------------------------------===//
770
771namespace preprocessoroutputoptions {
772
773static llvm::cl::opt<bool>
774DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
775
776static llvm::cl::opt<bool>
777EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
778
779static llvm::cl::opt<bool>
780EnableMacroCommentOutput("CC",
781 llvm::cl::desc("Enable comment output in -E mode, "
782 "even from macro expansions"));
783static llvm::cl::opt<bool>
784DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
785 " normal output"));
786static llvm::cl::opt<bool>
787DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
788 "addition to normal output"));
789
790}
791
792//===----------------------------------------------------------------------===//
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000793// Option Object Construction
794//===----------------------------------------------------------------------===//
795
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000796void clang::InitializeCodeGenOptions(CodeGenOptions &Opts,
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000797 const TargetInfo &Target) {
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000798 using namespace codegenoptions;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000799
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000800 // Compute the target features, we need the target to handle this because
801 // features may have dependencies on one another.
802 llvm::StringMap<bool> Features;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000803 Target.getDefaultFeatures(TargetCPU, Features);
804
805 // Apply the user specified deltas.
806 for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
807 ie = TargetFeatures.end(); it != ie; ++it) {
808 const char *Name = it->c_str();
809
810 // FIXME: Don't handle errors like this.
811 if (Name[0] != '-' && Name[0] != '+') {
812 fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
813 Name);
814 exit(1);
815 }
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000816
817 // Apply the feature via the target.
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000818 if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
819 fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
820 Name + 1);
821 exit(1);
822 }
823 }
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000824
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000825 // Add the features to the compile options.
826 //
827 // FIXME: If we are completely confident that we have the right set, we only
828 // need to pass the minuses.
829 for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
830 ie = Features.end(); it != ie; ++it)
831 Opts.Features.push_back(std::string(it->second ? "+" : "-") + it->first());
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000832
833 // -Os implies -O2
834 Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
835
836 // We must always run at least the always inlining pass.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000837 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CodeGenOptions::NormalInlining
838 : CodeGenOptions::OnlyAlwaysInlining;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000839
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000840 Opts.CPU = TargetCPU;
841 Opts.DebugInfo = GenerateDebugInfo;
842 Opts.DisableLLVMOpts = DisableLLVMOptimizations;
843 Opts.DisableRedZone = DisableRedZone;
844 Opts.MergeAllConstants = !NoMergeConstants;
845 Opts.NoCommon = NoCommon;
846 Opts.NoImplicitFloat = NoImplicitFloat;
847 Opts.OptimizeSize = OptSize;
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000848 Opts.SimplifyLibCalls = 1;
Daniel Dunbar29a790b2009-11-11 09:38:56 +0000849 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000850
851#ifdef NDEBUG
852 Opts.VerifyModule = 0;
853#endif
Daniel Dunbar0498cfc2009-11-10 19:51:53 +0000854}
Daniel Dunbarb52d2432009-11-11 06:10:03 +0000855
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000856void clang::InitializeDependencyOutputOptions(DependencyOutputOptions &Opts) {
857 using namespace dependencyoutputoptions;
858
859 Opts.OutputFile = DependencyFile;
Daniel Dunbar26266882009-11-12 23:52:32 +0000860 Opts.Targets = DependencyTargets;
Daniel Dunbar0e0bae82009-11-11 21:43:12 +0000861 Opts.IncludeSystemHeaders = DependenciesIncludeSystemHeaders;
862 Opts.UsePhonyTargets = PhonyDependencyTarget;
863}
864
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000865void clang::InitializeDiagnosticOptions(DiagnosticOptions &Opts) {
866 using namespace diagnosticoptions;
867
Daniel Dunbar26266882009-11-12 23:52:32 +0000868 Opts.Warnings = OptWarnings;
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000869 Opts.DumpBuildInformation = DumpBuildInformation;
Daniel Dunbar69079432009-11-12 07:28:44 +0000870 Opts.IgnoreWarnings = OptNoWarnings;
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000871 Opts.MessageLength = MessageLength;
Daniel Dunbar69079432009-11-12 07:28:44 +0000872 Opts.NoRewriteMacros = SilenceRewriteMacroWarning;
873 Opts.Pedantic = OptPedantic;
874 Opts.PedanticErrors = OptPedanticErrors;
Daniel Dunbar11e729d2009-11-12 07:28:21 +0000875 Opts.ShowCarets = !NoCaretDiagnostics;
876 Opts.ShowColors = PrintColorDiagnostic;
877 Opts.ShowColumn = !NoShowColumn;
878 Opts.ShowFixits = !NoDiagnosticsFixIt;
879 Opts.ShowLocation = !NoShowLocation;
880 Opts.ShowOptionNames = PrintDiagnosticOption;
881 Opts.ShowSourceRanges = PrintSourceRangeInfo;
Daniel Dunbar26266882009-11-12 23:52:32 +0000882 Opts.VerifyDiagnostics = VerifyDiagnostics;
883}
884
885void clang::InitializeFrontendOptions(FrontendOptions &Opts) {
886 using namespace frontendoptions;
887
Daniel Dunbar914474c2009-11-13 01:02:10 +0000888 Opts.CodeCompletionAt = CodeCompletionAt;
889 Opts.DebugCodeCompletionPrinter = CodeCompletionDebugPrinter;
Daniel Dunbar26266882009-11-12 23:52:32 +0000890 Opts.DisableFree = DisableFree;
891 Opts.EmptyInputOnly = EmptyInputOnly;
Daniel Dunbarc86804b2009-11-12 23:52:56 +0000892 Opts.FixItLocations = FixItAtLocations;
Daniel Dunbar9a8a83b2009-11-14 22:32:38 +0000893 Opts.ProgramAction = ProgAction;
Daniel Dunbar26266882009-11-12 23:52:32 +0000894 Opts.OutputFile = OutputFile;
Daniel Dunbar914474c2009-11-13 01:02:10 +0000895 Opts.RelocatablePCH = RelocatablePCH;
896 Opts.ShowMacrosInCodeCompletion = CodeCompletionWantsMacros;
897 Opts.ShowStats = Stats;
898 Opts.ShowTimers = TimeReport;
Daniel Dunbar21dac5e2009-11-13 01:02:19 +0000899 Opts.TargetABI = TargetABI;
900 Opts.TargetTriple = TargetTriple;
Daniel Dunbar26266882009-11-12 23:52:32 +0000901 Opts.ViewClassInheritance = InheritanceViewCls;
902
903 // '-' is the default input if none is given.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000904 if (InputFilenames.empty()) {
905 FrontendOptions::InputKind IK = InputType;
906 if (IK == FrontendOptions::IK_None) IK = FrontendOptions::IK_C;
907 Opts.Inputs.push_back(std::make_pair(IK, "-"));
908 } else {
909 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
910 FrontendOptions::InputKind IK = InputType;
911 llvm::StringRef Ext =
912 llvm::StringRef(InputFilenames[i]).rsplit('.').second;
913 if (IK == FrontendOptions::IK_None)
914 IK = FrontendOptions::getInputKindForExtension(Ext);
915 Opts.Inputs.push_back(std::make_pair(IK, InputFilenames[i]));
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +0000916 }
917 }
Daniel Dunbar0db4b762009-11-11 08:13:40 +0000918}
919
Daniel Dunbarf7973292009-11-11 08:13:32 +0000920void clang::InitializeHeaderSearchOptions(HeaderSearchOptions &Opts,
921 llvm::StringRef BuiltinIncludePath,
Daniel Dunbarf7973292009-11-11 08:13:32 +0000922 const LangOptions &Lang) {
923 using namespace headersearchoptions;
924
925 Opts.Sysroot = isysroot;
926 Opts.Verbose = Verbose;
927
928 // Handle -I... and -F... options, walking the lists in parallel.
929 unsigned Iidx = 0, Fidx = 0;
930 while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) {
931 if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) {
932 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
933 ++Iidx;
934 } else {
935 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
936 ++Fidx;
937 }
938 }
939
940 // Consume what's left from whatever list was longer.
941 for (; Iidx != I_dirs.size(); ++Iidx)
942 Opts.AddPath(I_dirs[Iidx], frontend::Angled, false, true, false);
943 for (; Fidx != F_dirs.size(); ++Fidx)
944 Opts.AddPath(F_dirs[Fidx], frontend::Angled, false, true, true);
945
946 // Handle -idirafter... options.
947 for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
948 Opts.AddPath(idirafter_dirs[i], frontend::After,
949 false, true, false);
950
951 // Handle -iquote... options.
952 for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
953 Opts.AddPath(iquote_dirs[i], frontend::Quoted, false, true, false);
954
955 // Handle -isystem... options.
956 for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
957 Opts.AddPath(isystem_dirs[i], frontend::System, false, true, false);
958
959 // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
960 // parallel, processing the values in order of occurance to get the right
961 // prefixes.
962 {
963 std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
964 unsigned iprefix_idx = 0;
965 unsigned iwithprefix_idx = 0;
966 unsigned iwithprefixbefore_idx = 0;
967 bool iprefix_done = iprefix_vals.empty();
968 bool iwithprefix_done = iwithprefix_vals.empty();
969 bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
970 while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
971 if (!iprefix_done &&
972 (iwithprefix_done ||
973 iprefix_vals.getPosition(iprefix_idx) <
974 iwithprefix_vals.getPosition(iwithprefix_idx)) &&
975 (iwithprefixbefore_done ||
976 iprefix_vals.getPosition(iprefix_idx) <
977 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
978 Prefix = iprefix_vals[iprefix_idx];
979 ++iprefix_idx;
980 iprefix_done = iprefix_idx == iprefix_vals.size();
981 } else if (!iwithprefix_done &&
982 (iwithprefixbefore_done ||
983 iwithprefix_vals.getPosition(iwithprefix_idx) <
984 iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
985 Opts.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
986 frontend::System, false, false, false);
987 ++iwithprefix_idx;
988 iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
989 } else {
990 Opts.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
991 frontend::Angled, false, false, false);
992 ++iwithprefixbefore_idx;
993 iwithprefixbefore_done =
994 iwithprefixbefore_idx == iwithprefixbefore_vals.size();
995 }
996 }
997 }
998
999 // Add CPATH environment paths.
1000 if (const char *Env = getenv("CPATH"))
1001 Opts.EnvIncPath = Env;
1002
1003 // Add language specific environment paths.
1004 if (Lang.CPlusPlus && Lang.ObjC1) {
1005 if (const char *Env = getenv("OBJCPLUS_INCLUDE_PATH"))
1006 Opts.LangEnvIncPath = Env;
1007 } else if (Lang.CPlusPlus) {
1008 if (const char *Env = getenv("CPLUS_INCLUDE_PATH"))
1009 Opts.LangEnvIncPath = Env;
1010 } else if (Lang.ObjC1) {
1011 if (const char *Env = getenv("OBJC_INCLUDE_PATH"))
1012 Opts.LangEnvIncPath = Env;
1013 } else {
1014 if (const char *Env = getenv("C_INCLUDE_PATH"))
1015 Opts.LangEnvIncPath = Env;
1016 }
1017
1018 if (!nobuiltininc)
1019 Opts.BuiltinIncludePath = BuiltinIncludePath;
1020
1021 Opts.UseStandardIncludes = !nostdinc;
1022}
1023
Daniel Dunbarb52d2432009-11-11 06:10:03 +00001024void clang::InitializePreprocessorOptions(PreprocessorOptions &Opts) {
1025 using namespace preprocessoroptions;
1026
1027 Opts.setImplicitPCHInclude(ImplicitIncludePCH);
1028 Opts.setImplicitPTHInclude(ImplicitIncludePTH);
1029
Daniel Dunbarb3cb98e2009-11-12 02:53:59 +00001030 // Select the token cache file, we don't support more than one currently so we
1031 // can't have both an implicit-pth and a token cache file.
1032 if (TokenCache.getPosition() && ImplicitIncludePTH.getPosition()) {
1033 // FIXME: Don't fail like this.
1034 fprintf(stderr, "error: cannot use both -token-cache and -include-pth "
1035 "options\n");
1036 exit(1);
1037 }
1038 if (TokenCache.getPosition())
1039 Opts.setTokenCache(TokenCache);
1040 else
1041 Opts.setTokenCache(ImplicitIncludePTH);
1042
Daniel Dunbarb52d2432009-11-11 06:10:03 +00001043 // Use predefines?
1044 Opts.setUsePredefines(!UndefMacros);
1045
1046 // Add macros from the command line.
1047 unsigned d = 0, D = D_macros.size();
1048 unsigned u = 0, U = U_macros.size();
1049 while (d < D || u < U) {
1050 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
1051 Opts.addMacroDef(D_macros[d++]);
1052 else
1053 Opts.addMacroUndef(U_macros[u++]);
1054 }
1055
1056 // If -imacros are specified, include them now. These are processed before
1057 // any -include directives.
1058 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
1059 Opts.addMacroInclude(ImplicitMacroIncludes[i]);
1060
1061 // Add the ordered list of -includes, sorting in the implicit include options
1062 // at the appropriate location.
1063 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
1064 std::string OriginalFile;
1065
1066 if (!ImplicitIncludePTH.empty())
1067 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
1068 &ImplicitIncludePTH));
1069 if (!ImplicitIncludePCH.empty()) {
1070 OriginalFile = PCHReader::getOriginalSourceFile(ImplicitIncludePCH);
1071 // FIXME: Don't fail like this.
1072 if (OriginalFile.empty())
1073 exit(1);
1074 OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
1075 &OriginalFile));
1076 }
1077 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
1078 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
1079 &ImplicitIncludes[i]));
1080 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
1081
1082 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
1083 Opts.addInclude(*OrderedPaths[i].second);
1084}
Daniel Dunbar56749082009-11-11 07:26:12 +00001085
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001086void clang::InitializeLangOptions(LangOptions &Options,
1087 FrontendOptions::InputKind IK,
Daniel Dunbar56749082009-11-11 07:26:12 +00001088 TargetInfo &Target,
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001089 const CodeGenOptions &CodeGenOpts) {
Daniel Dunbar56749082009-11-11 07:26:12 +00001090 using namespace langoptions;
1091
1092 bool NoPreprocess = false;
1093
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001094 switch (IK) {
1095 case FrontendOptions::IK_None:
1096 case FrontendOptions::IK_AST:
1097 assert(0 && "Invalid input kind!");
1098 case FrontendOptions::IK_Asm:
Daniel Dunbar56749082009-11-11 07:26:12 +00001099 Options.AsmPreprocessor = 1;
1100 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001101 case FrontendOptions::IK_PreprocessedC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001102 NoPreprocess = true;
1103 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001104 case FrontendOptions::IK_C:
Daniel Dunbar56749082009-11-11 07:26:12 +00001105 // Do nothing.
1106 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001107 case FrontendOptions::IK_PreprocessedCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001108 NoPreprocess = true;
1109 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001110 case FrontendOptions::IK_CXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001111 Options.CPlusPlus = 1;
1112 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001113 case FrontendOptions::IK_PreprocessedObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001114 NoPreprocess = true;
1115 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001116 case FrontendOptions::IK_ObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001117 Options.ObjC1 = Options.ObjC2 = 1;
1118 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001119 case FrontendOptions::IK_PreprocessedObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001120 NoPreprocess = true;
1121 // FALLTHROUGH
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001122 case FrontendOptions::IK_ObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001123 Options.ObjC1 = Options.ObjC2 = 1;
1124 Options.CPlusPlus = 1;
1125 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001126 case FrontendOptions::IK_OpenCL:
Daniel Dunbar56749082009-11-11 07:26:12 +00001127 Options.OpenCL = 1;
1128 Options.AltiVec = 1;
1129 Options.CXXOperatorNames = 1;
1130 Options.LaxVectorConversions = 1;
1131 break;
1132 }
1133
1134 if (ObjCExclusiveGC)
1135 Options.setGCMode(LangOptions::GCOnly);
1136 else if (ObjCEnableGC)
1137 Options.setGCMode(LangOptions::HybridGC);
1138
1139 if (ObjCEnableGCBitmapPrint)
1140 Options.ObjCGCBitmapPrint = 1;
1141
1142 if (AltiVec)
1143 Options.AltiVec = 1;
1144
1145 if (PThread)
1146 Options.POSIXThreads = 1;
1147
1148 Options.setVisibilityMode(SymbolVisibility);
1149 Options.OverflowChecking = OverflowChecking;
1150
1151
1152 // Allow the target to set the default the language options as it sees fit.
1153 Target.getDefaultLangOptions(Options);
1154
1155 // Pass the map of target features to the target for validation and
1156 // processing.
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001157 Target.HandleTargetFeatures(CodeGenOpts.Features);
Daniel Dunbar56749082009-11-11 07:26:12 +00001158
1159 if (LangStd == lang_unspecified) {
1160 // Based on the base language, pick one.
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001161 switch (IK) {
1162 case FrontendOptions::IK_None:
1163 case FrontendOptions::IK_AST:
1164 assert(0 && "Invalid input kind!");
1165 case FrontendOptions::IK_OpenCL:
Daniel Dunbar56749082009-11-11 07:26:12 +00001166 LangStd = lang_c99;
1167 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001168 case FrontendOptions::IK_Asm:
1169 case FrontendOptions::IK_C:
1170 case FrontendOptions::IK_PreprocessedC:
1171 case FrontendOptions::IK_ObjC:
1172 case FrontendOptions::IK_PreprocessedObjC:
Daniel Dunbar56749082009-11-11 07:26:12 +00001173 LangStd = lang_gnu99;
1174 break;
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001175 case FrontendOptions::IK_CXX:
1176 case FrontendOptions::IK_PreprocessedCXX:
1177 case FrontendOptions::IK_ObjCXX:
1178 case FrontendOptions::IK_PreprocessedObjCXX:
Daniel Dunbar56749082009-11-11 07:26:12 +00001179 LangStd = lang_gnucxx98;
1180 break;
1181 }
1182 }
1183
1184 switch (LangStd) {
1185 default: assert(0 && "Unknown language standard!");
1186
1187 // Fall through from newer standards to older ones. This isn't really right.
1188 // FIXME: Enable specifically the right features based on the language stds.
1189 case lang_gnucxx0x:
1190 case lang_cxx0x:
1191 Options.CPlusPlus0x = 1;
1192 // FALL THROUGH
1193 case lang_gnucxx98:
1194 case lang_cxx98:
1195 Options.CPlusPlus = 1;
1196 Options.CXXOperatorNames = !NoOperatorNames;
1197 // FALL THROUGH.
1198 case lang_gnu99:
1199 case lang_c99:
1200 Options.C99 = 1;
1201 Options.HexFloats = 1;
1202 // FALL THROUGH.
1203 case lang_gnu89:
1204 Options.BCPLComment = 1; // Only for C99/C++.
1205 // FALL THROUGH.
1206 case lang_c94:
1207 Options.Digraphs = 1; // C94, C99, C++.
1208 // FALL THROUGH.
1209 case lang_c89:
1210 break;
1211 }
1212
1213 // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc.
1214 switch (LangStd) {
1215 default: assert(0 && "Unknown language standard!");
1216 case lang_gnucxx0x:
1217 case lang_gnucxx98:
1218 case lang_gnu99:
1219 case lang_gnu89:
1220 Options.GNUMode = 1;
1221 break;
1222 case lang_cxx0x:
1223 case lang_cxx98:
1224 case lang_c99:
1225 case lang_c94:
1226 case lang_c89:
1227 Options.GNUMode = 0;
1228 break;
1229 }
1230
1231 if (Options.CPlusPlus) {
1232 Options.C99 = 0;
1233 Options.HexFloats = 0;
1234 }
1235
1236 if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89)
1237 Options.ImplicitInt = 1;
1238 else
1239 Options.ImplicitInt = 0;
1240
1241 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
1242 // is specified, or -std is set to a conforming mode.
1243 Options.Trigraphs = !Options.GNUMode;
1244 if (Trigraphs.getPosition())
1245 Options.Trigraphs = Trigraphs; // Command line option wins if specified.
1246
1247 // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off
1248 // even if they are normally on for the target. In GNU modes (e.g.
1249 // -std=gnu99) the default for blocks depends on the target settings.
1250 // However, blocks are not turned off when compiling Obj-C or Obj-C++ code.
1251 if (!Options.ObjC1 && !Options.GNUMode)
1252 Options.Blocks = 0;
1253
1254 // Default to not accepting '$' in identifiers when preprocessing assembler,
1255 // but do accept when preprocessing C. FIXME: these defaults are right for
1256 // darwin, are they right everywhere?
Daniel Dunbarfbe2faf2009-11-13 02:06:12 +00001257 Options.DollarIdents = IK != FrontendOptions::IK_Asm;
Daniel Dunbar56749082009-11-11 07:26:12 +00001258 if (DollarsInIdents.getPosition()) // Explicit setting overrides default.
1259 Options.DollarIdents = DollarsInIdents;
1260
1261 if (PascalStrings.getPosition())
1262 Options.PascalStrings = PascalStrings;
1263 if (MSExtensions.getPosition())
1264 Options.Microsoft = MSExtensions;
1265 Options.WritableStrings = WritableStrings;
1266 if (NoLaxVectorConversions.getPosition())
1267 Options.LaxVectorConversions = 0;
1268 Options.Exceptions = Exceptions;
1269 Options.Rtti = Rtti;
1270 if (EnableBlocks.getPosition())
1271 Options.Blocks = EnableBlocks;
1272 if (CharIsSigned.getPosition())
1273 Options.CharIsSigned = CharIsSigned;
1274 if (ShortWChar.getPosition())
1275 Options.ShortWChar = ShortWChar;
1276
1277 if (!AllowBuiltins)
1278 Options.NoBuiltin = 1;
1279 if (Freestanding)
1280 Options.Freestanding = Options.NoBuiltin = 1;
1281
1282 if (EnableHeinousExtensions)
1283 Options.HeinousExtensions = 1;
1284
1285 if (AccessControl)
1286 Options.AccessControl = 1;
1287
1288 Options.ElideConstructors = !NoElideConstructors;
1289
1290 // OpenCL and C++ both have bool, true, false keywords.
1291 Options.Bool = Options.OpenCL | Options.CPlusPlus;
1292
1293 Options.MathErrno = MathErrno;
1294
1295 Options.InstantiationDepth = TemplateDepth;
1296
1297 // Override the default runtime if the user requested it.
1298 if (NeXTRuntime)
1299 Options.NeXTRuntime = 1;
1300 else if (GNURuntime)
1301 Options.NeXTRuntime = 0;
1302
1303 if (!ObjCConstantStringClass.empty())
1304 Options.ObjCConstantStringClass = ObjCConstantStringClass.c_str();
1305
1306 if (ObjCNonFragileABI)
1307 Options.ObjCNonFragileABI = 1;
1308
1309 if (EmitAllDecls)
1310 Options.EmitAllDecls = 1;
1311
1312 // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't support.
1313 Options.OptimizeSize = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001314 Options.Optimize = !!CodeGenOpts.OptimizationLevel;
Daniel Dunbar56749082009-11-11 07:26:12 +00001315
1316 assert(PICLevel <= 2 && "Invalid value for -pic-level");
1317 Options.PICLevel = PICLevel;
1318
1319 Options.GNUInline = !Options.C99;
1320 // FIXME: This is affected by other options (-fno-inline).
1321
1322 // This is the __NO_INLINE__ define, which just depends on things like the
1323 // optimization level and -fno-inline, not actually whether the backend has
1324 // inlining enabled.
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001325 Options.NoInline = !CodeGenOpts.OptimizationLevel;
Daniel Dunbar56749082009-11-11 07:26:12 +00001326
1327 Options.Static = StaticDefine;
1328
1329 switch (StackProtector) {
1330 default:
1331 assert(StackProtector <= 2 && "Invalid value for -stack-protector");
1332 case -1: break;
1333 case 0: Options.setStackProtectorMode(LangOptions::SSPOff); break;
1334 case 1: Options.setStackProtectorMode(LangOptions::SSPOn); break;
1335 case 2: Options.setStackProtectorMode(LangOptions::SSPReq); break;
1336 }
1337
1338 if (MainFileName.getPosition())
1339 Options.setMainFileName(MainFileName.c_str());
1340
1341 Target.setForcedLangOptions(Options);
1342}
Daniel Dunbar29cf7462009-11-11 10:07:44 +00001343
1344void
1345clang::InitializePreprocessorOutputOptions(PreprocessorOutputOptions &Opts) {
1346 using namespace preprocessoroutputoptions;
1347
1348 Opts.ShowCPP = !DumpMacros;
1349 Opts.ShowMacros = DumpMacros || DumpDefines;
1350 Opts.ShowLineMarkers = !DisableLineMarkers;
1351 Opts.ShowComments = EnableCommentOutput;
1352 Opts.ShowMacroComments = EnableMacroCommentOutput;
1353}