Daniel Dunbar | f89a32a | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 1 | //===--- 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 Dunbar | 999215c | 2009-11-11 06:10:03 +0000 | [diff] [blame^] | 16 | #include "clang/Frontend/PCHReader.h" |
| 17 | #include "clang/Frontend/PreprocessorOptions.h" |
Daniel Dunbar | f89a32a | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 999215c | 2009-11-11 06:10:03 +0000 | [diff] [blame^] | 20 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | f89a32a | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | ad5ef3d | 2009-11-10 21:21:27 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
Daniel Dunbar | f89a32a | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Code Generation Options |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | namespace codegenoptions { |
| 32 | |
| 33 | static llvm::cl::opt<bool> |
| 34 | DisableLLVMOptimizations("disable-llvm-optzns", |
| 35 | llvm::cl::desc("Don't run LLVM optimization passes")); |
| 36 | |
| 37 | static llvm::cl::opt<bool> |
| 38 | DisableRedZone("disable-red-zone", |
| 39 | llvm::cl::desc("Do not emit code that uses the red zone."), |
| 40 | llvm::cl::init(false)); |
| 41 | |
| 42 | static llvm::cl::opt<bool> |
| 43 | GenerateDebugInfo("g", |
| 44 | llvm::cl::desc("Generate source level debug information")); |
| 45 | |
| 46 | static llvm::cl::opt<bool> |
| 47 | NoCommon("fno-common", |
| 48 | llvm::cl::desc("Compile common globals like normal definitions"), |
| 49 | llvm::cl::ValueDisallowed); |
| 50 | |
| 51 | static llvm::cl::opt<bool> |
| 52 | NoImplicitFloat("no-implicit-float", |
| 53 | llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"), |
| 54 | llvm::cl::init(false)); |
| 55 | |
| 56 | static llvm::cl::opt<bool> |
| 57 | NoMergeConstants("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. |
| 61 | struct 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 | }; |
| 71 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 72 | OptLevel("O", llvm::cl::Prefix, |
| 73 | llvm::cl::desc("Optimization level"), |
| 74 | llvm::cl::init(0)); |
| 75 | |
| 76 | static llvm::cl::opt<bool> |
| 77 | OptSize("Os", llvm::cl::desc("Optimize for size")); |
| 78 | |
| 79 | static llvm::cl::opt<std::string> |
| 80 | TargetCPU("mcpu", |
| 81 | llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)")); |
| 82 | |
| 83 | static llvm::cl::list<std::string> |
| 84 | TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes")); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 999215c | 2009-11-11 06:10:03 +0000 | [diff] [blame^] | 89 | // General Preprocessor Options |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | |
| 92 | namespace preprocessoroptions { |
| 93 | |
| 94 | static llvm::cl::list<std::string> |
| 95 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 96 | llvm::cl::desc("Predefine the specified macro")); |
| 97 | |
| 98 | static llvm::cl::list<std::string> |
| 99 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 100 | llvm::cl::desc("Include file before parsing")); |
| 101 | static llvm::cl::list<std::string> |
| 102 | ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"), |
| 103 | llvm::cl::desc("Include macros from file before parsing")); |
| 104 | |
| 105 | static llvm::cl::opt<std::string> |
| 106 | ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"), |
| 107 | llvm::cl::desc("Include precompiled header file")); |
| 108 | |
| 109 | static llvm::cl::opt<std::string> |
| 110 | ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"), |
| 111 | llvm::cl::desc("Include file before parsing")); |
| 112 | |
| 113 | static llvm::cl::list<std::string> |
| 114 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 115 | llvm::cl::desc("Undefine the specified macro")); |
| 116 | |
| 117 | static llvm::cl::opt<bool> |
| 118 | UndefMacros("undef", llvm::cl::value_desc("macro"), |
| 119 | llvm::cl::desc("undef all system defines")); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f89a32a | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 124 | // 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. |
| 130 | void 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 | |
| 157 | void 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 Dunbar | 999215c | 2009-11-11 06:10:03 +0000 | [diff] [blame^] | 196 | |
| 197 | void 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 | } |