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