blob: 8105875d22bfa481963bf0c7f15bf55a4ff9fe3c [file] [log] [blame]
Daniel Dunbarf89a32a2009-11-10 19:51:53 +00001//===--- Options.cpp - clang-cc Option Handling ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// This file contains "pure" option handling, it is only responsible for turning
11// the options into internal *Option classes, but shouldn't have any other
12// logic.
13
14#include "Options.h"
15#include "clang/Frontend/CompileOptions.h"
Daniel Dunbar999215c2009-11-11 06:10:03 +000016#include "clang/Frontend/PCHReader.h"
17#include "clang/Frontend/PreprocessorOptions.h"
Daniel Dunbarf89a32a2009-11-10 19:51:53 +000018#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/TargetInfo.h"
Daniel Dunbar999215c2009-11-11 06:10:03 +000020#include "llvm/ADT/STLExtras.h"
Daniel Dunbarf89a32a2009-11-10 19:51:53 +000021#include "llvm/ADT/StringMap.h"
22#include "llvm/Support/CommandLine.h"
Dan Gohmanad5ef3d2009-11-10 21:21:27 +000023#include <stdio.h>
Daniel Dunbarf89a32a2009-11-10 19:51:53 +000024
25using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Code Generation Options
29//===----------------------------------------------------------------------===//
30
31namespace codegenoptions {
32
33static llvm::cl::opt<bool>
34DisableLLVMOptimizations("disable-llvm-optzns",
35 llvm::cl::desc("Don't run LLVM optimization passes"));
36
37static llvm::cl::opt<bool>
38DisableRedZone("disable-red-zone",
39 llvm::cl::desc("Do not emit code that uses the red zone."),
40 llvm::cl::init(false));
41
42static llvm::cl::opt<bool>
43GenerateDebugInfo("g",
44 llvm::cl::desc("Generate source level debug information"));
45
46static llvm::cl::opt<bool>
47NoCommon("fno-common",
48 llvm::cl::desc("Compile common globals like normal definitions"),
49 llvm::cl::ValueDisallowed);
50
51static llvm::cl::opt<bool>
52NoImplicitFloat("no-implicit-float",
53 llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"),
54 llvm::cl::init(false));
55
56static llvm::cl::opt<bool>
57NoMergeConstants("fno-merge-all-constants",
58 llvm::cl::desc("Disallow merging of constants."));
59
60// It might be nice to add bounds to the CommandLine library directly.
61struct OptLevelParser : public llvm::cl::parser<unsigned> {
62 bool parse(llvm::cl::Option &O, llvm::StringRef ArgName,
63 llvm::StringRef Arg, unsigned &Val) {
64 if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
65 return true;
66 if (Val > 3)
67 return O.error("'" + Arg + "' invalid optimization level!");
68 return false;
69 }
70};
71static llvm::cl::opt<unsigned, false, OptLevelParser>
72OptLevel("O", llvm::cl::Prefix,
73 llvm::cl::desc("Optimization level"),
74 llvm::cl::init(0));
75
76static llvm::cl::opt<bool>
77OptSize("Os", llvm::cl::desc("Optimize for size"));
78
79static llvm::cl::opt<std::string>
80TargetCPU("mcpu",
81 llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));
82
83static llvm::cl::list<std::string>
84TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes"));
85
86}
87
88//===----------------------------------------------------------------------===//
Daniel Dunbar999215c2009-11-11 06:10:03 +000089// General Preprocessor Options
90//===----------------------------------------------------------------------===//
91
92namespace preprocessoroptions {
93
94static llvm::cl::list<std::string>
95D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
96 llvm::cl::desc("Predefine the specified macro"));
97
98static llvm::cl::list<std::string>
99ImplicitIncludes("include", llvm::cl::value_desc("file"),
100 llvm::cl::desc("Include file before parsing"));
101static llvm::cl::list<std::string>
102ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"),
103 llvm::cl::desc("Include macros from file before parsing"));
104
105static llvm::cl::opt<std::string>
106ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"),
107 llvm::cl::desc("Include precompiled header file"));
108
109static llvm::cl::opt<std::string>
110ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"),
111 llvm::cl::desc("Include file before parsing"));
112
113static llvm::cl::list<std::string>
114U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
115 llvm::cl::desc("Undefine the specified macro"));
116
117static llvm::cl::opt<bool>
118UndefMacros("undef", llvm::cl::value_desc("macro"),
119 llvm::cl::desc("undef all system defines"));
120
121}
122
123//===----------------------------------------------------------------------===//
Daniel Dunbarf89a32a2009-11-10 19:51:53 +0000124// Option Object Construction
125//===----------------------------------------------------------------------===//
126
127/// ComputeTargetFeatures - Recompute the target feature list to only
128/// be the list of things that are enabled, based on the target cpu
129/// and feature list.
130void clang::ComputeFeatureMap(TargetInfo &Target,
131 llvm::StringMap<bool> &Features) {
132 using namespace codegenoptions;
133 assert(Features.empty() && "invalid map");
134
135 // Initialize the feature map based on the target.
136 Target.getDefaultFeatures(TargetCPU, Features);
137
138 // Apply the user specified deltas.
139 for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(),
140 ie = TargetFeatures.end(); it != ie; ++it) {
141 const char *Name = it->c_str();
142
143 // FIXME: Don't handle errors like this.
144 if (Name[0] != '-' && Name[0] != '+') {
145 fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n",
146 Name);
147 exit(1);
148 }
149 if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) {
150 fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n",
151 Name + 1);
152 exit(1);
153 }
154 }
155}
156
157void clang::InitializeCompileOptions(CompileOptions &Opts,
158 const llvm::StringMap<bool> &Features) {
159 using namespace codegenoptions;
160 Opts.OptimizeSize = OptSize;
161 Opts.DebugInfo = GenerateDebugInfo;
162 Opts.DisableLLVMOpts = DisableLLVMOptimizations;
163
164 // -Os implies -O2
165 Opts.OptimizationLevel = OptSize ? 2 : OptLevel;
166
167 // We must always run at least the always inlining pass.
168 Opts.Inlining = (Opts.OptimizationLevel > 1) ? CompileOptions::NormalInlining
169 : CompileOptions::OnlyAlwaysInlining;
170
171 Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize);
172 Opts.SimplifyLibCalls = 1;
173
174#ifdef NDEBUG
175 Opts.VerifyModule = 0;
176#endif
177
178 Opts.CPU = TargetCPU;
179 Opts.Features.clear();
180 for (llvm::StringMap<bool>::const_iterator it = Features.begin(),
181 ie = Features.end(); it != ie; ++it) {
182 // FIXME: If we are completely confident that we have the right set, we only
183 // need to pass the minuses.
184 std::string Name(it->second ? "+" : "-");
185 Name += it->first();
186 Opts.Features.push_back(Name);
187 }
188
189 Opts.NoCommon = NoCommon;
190
191 Opts.DisableRedZone = DisableRedZone;
192 Opts.NoImplicitFloat = NoImplicitFloat;
193
194 Opts.MergeAllConstants = !NoMergeConstants;
195}
Daniel Dunbar999215c2009-11-11 06:10:03 +0000196
197void clang::InitializePreprocessorOptions(PreprocessorOptions &Opts) {
198 using namespace preprocessoroptions;
199
200 Opts.setImplicitPCHInclude(ImplicitIncludePCH);
201 Opts.setImplicitPTHInclude(ImplicitIncludePTH);
202
203 // Use predefines?
204 Opts.setUsePredefines(!UndefMacros);
205
206 // Add macros from the command line.
207 unsigned d = 0, D = D_macros.size();
208 unsigned u = 0, U = U_macros.size();
209 while (d < D || u < U) {
210 if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u)))
211 Opts.addMacroDef(D_macros[d++]);
212 else
213 Opts.addMacroUndef(U_macros[u++]);
214 }
215
216 // If -imacros are specified, include them now. These are processed before
217 // any -include directives.
218 for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i)
219 Opts.addMacroInclude(ImplicitMacroIncludes[i]);
220
221 // Add the ordered list of -includes, sorting in the implicit include options
222 // at the appropriate location.
223 llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths;
224 std::string OriginalFile;
225
226 if (!ImplicitIncludePTH.empty())
227 OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(),
228 &ImplicitIncludePTH));
229 if (!ImplicitIncludePCH.empty()) {
230 OriginalFile = PCHReader::getOriginalSourceFile(ImplicitIncludePCH);
231 // FIXME: Don't fail like this.
232 if (OriginalFile.empty())
233 exit(1);
234 OrderedPaths.push_back(std::make_pair(ImplicitIncludePCH.getPosition(),
235 &OriginalFile));
236 }
237 for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i)
238 OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i),
239 &ImplicitIncludes[i]));
240 llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end());
241
242 for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i)
243 Opts.addInclude(*OrderedPaths[i].second);
244}