 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
83.3% |
110 / 132 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
93.9% |
124 / 132 |
| |
|
Line Coverage: |
94.2% |
258 / 274 |
| |
 |
|
 |
1 : //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
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 implements the clang::InitializePreprocessor function.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "clang/Frontend/Utils.h"
15 : #include "clang/Basic/MacroBuilder.h"
16 : #include "clang/Basic/TargetInfo.h"
17 : #include "clang/Frontend/FrontendDiagnostic.h"
18 : #include "clang/Frontend/FrontendOptions.h"
19 : #include "clang/Frontend/PreprocessorOptions.h"
20 : #include "clang/Lex/Preprocessor.h"
21 : #include "clang/Basic/FileManager.h"
22 : #include "clang/Basic/SourceManager.h"
23 : #include "llvm/ADT/APFloat.h"
24 : #include "llvm/Support/MemoryBuffer.h"
25 : #include "llvm/System/Path.h"
26 : using namespace clang;
27 :
28 : // Append a #define line to Buf for Macro. Macro should be of the form XXX,
29 : // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
30 : // "#define XXX Y z W". To get a #define with no value, use "XXX=".
31 : static void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro,
32 78: Diagnostic &Diags) {
33 78: std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('=');
34 78: llvm::StringRef MacroName = MacroPair.first;
35 78: llvm::StringRef MacroBody = MacroPair.second;
46: branch 2 taken
32: branch 3 taken
36 78: if (MacroName.size() != Macro.size()) {
37 : // Per GCC -D semantics, the macro ends at \n if it exists.
38 46: llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r");
1: branch 0 taken
45: branch 1 taken
39 46: if (End != llvm::StringRef::npos)
40 : Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
41 1: << MacroName;
42 46: Builder.defineMacro(MacroName, MacroBody.substr(0, End));
43 : } else {
44 : // Push "macroname 1".
45 32: Builder.defineMacro(Macro);
46 : }
47 78: }
48 :
49 100: std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
50 : // Implicit include paths should be resolved relative to the current
51 : // working directory first, and then use the regular header search
52 : // mechanism. The proper way to handle this is to have the
53 : // predefines buffer located at the current working directory, but
54 : // it has not file entry. For now, workaround this by using an
55 : // absolute path if we find the file here, and otherwise letting
56 : // header search handle it.
57 100: llvm::sys::Path Path(File);
58 100: Path.makeAbsolute();
3: branch 1 taken
97: branch 2 taken
59 100: if (!Path.exists())
60 3: Path = File;
61 :
62 100: return Lexer::Stringify(Path.str());
63 : }
64 :
65 : /// AddImplicitInclude - Add an implicit #include of the specified file to the
66 : /// predefines buffer.
67 65: static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) {
68 : Builder.append("#include \"" +
69 65: llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
70 65: }
71 :
72 : static void AddImplicitIncludeMacros(MacroBuilder &Builder,
73 1: llvm::StringRef File) {
74 : Builder.append("#__include_macros \"" +
75 1: llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
76 : // Marker token to stop the __include_macros fetch loop.
77 1: Builder.append("##"); // ##?
78 1: }
79 :
80 : /// AddImplicitIncludePTH - Add an implicit #include using the original file
81 : /// used to generate a PTH cache.
82 : static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
83 1: llvm::StringRef ImplicitIncludePTH) {
84 1: PTHManager *P = PP.getPTHManager();
0: branch 0 not taken
1: branch 1 taken
85 1: assert(P && "No PTHManager.");
86 1: const char *OriginalFile = P->getOriginalSourceFile();
87 :
0: branch 0 not taken
1: branch 1 taken
88 1: if (!OriginalFile) {
89 : PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
90 0: << ImplicitIncludePTH;
91 0: return;
92 : }
93 :
94 1: AddImplicitInclude(Builder, OriginalFile);
95 : }
96 :
97 : /// PickFP - This is used to pick a value based on the FP semantics of the
98 : /// specified FP model.
99 : template <typename T>
100 : static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
101 : T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
102 78089: T IEEEQuadVal) {
15205: branch 0 taken
32656: branch 1 taken
10132: branch 2 taken
20096: branch 3 taken
103 78089: if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
104 25337: return IEEESingleVal;
15324: branch 0 taken
17332: branch 1 taken
10192: branch 2 taken
9904: branch 3 taken
105 52752: if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
106 25516: return IEEEDoubleVal;
17332: branch 0 taken
0: branch 1 not taken
9904: branch 2 taken
0: branch 3 not taken
107 27236: if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
108 27236: return X87DoubleExtendedVal;
0: branch 0 not taken
0: branch 1 not taken
0: branch 2 not taken
0: branch 3 not taken
109 0: if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
110 0: return PPCDoubleDoubleVal;
0: branch 0 not taken
0: branch 1 not taken
0: branch 3 not taken
0: branch 4 not taken
111 0: assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
112 0: return IEEEQuadVal;
113 : }
114 :
115 : static void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix,
116 7557: const llvm::fltSemantics *Sem) {
117 : const char *DenormMin, *Epsilon, *Max, *Min;
118 : DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
119 : "3.64519953188247460253e-4951L",
120 : "4.94065645841246544176568792868221e-324L",
121 7557: "6.47517511943802511092443895822764655e-4966L");
122 7557: int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
123 : Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
124 : "1.08420217248550443401e-19L",
125 : "4.94065645841246544176568792868221e-324L",
126 7557: "1.92592994438723585305597794258492732e-34L");
127 7557: int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
128 7557: int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
129 7557: int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
130 7557: int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
131 7557: int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
132 : Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
133 : "3.36210314311209350626e-4932L",
134 : "2.00416836000897277799610805135016e-292L",
135 7557: "3.36210314311209350626267781732175260e-4932L");
136 : Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
137 : "1.18973149535723176502e+4932L",
138 : "1.79769313486231580793728971405301e+308L",
139 7557: "1.18973149535723176508575932662800702e+4932L");
140 :
141 7557: llvm::SmallString<32> DefPrefix;
142 7557: DefPrefix = "__";
143 7557: DefPrefix += Prefix;
144 7557: DefPrefix += "_";
145 :
146 7557: Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
147 7557: Builder.defineMacro(DefPrefix + "HAS_DENORM__");
148 7557: Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits));
149 7557: Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon));
150 7557: Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
151 7557: Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
152 7557: Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits));
153 :
154 7557: Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp));
155 7557: Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp));
156 7557: Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max));
157 :
158 7557: Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")");
159 7557: Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")");
160 7557: Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min));
161 7557: }
162 :
163 :
164 : /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
165 : /// named MacroName with the max value for a type with width 'TypeWidth' a
166 : /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
167 : static void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth,
168 : llvm::StringRef ValSuffix, bool isSigned,
169 17633: MacroBuilder& Builder) {
170 : long long MaxVal;
17630: branch 0 taken
3: branch 1 taken
171 17633: if (isSigned)
172 17630: MaxVal = (1LL << (TypeWidth - 1)) - 1;
173 : else
174 3: MaxVal = ~0LL >> (64-TypeWidth);
175 :
176 17633: Builder.defineMacro(MacroName, llvm::Twine(MaxVal) + ValSuffix);
177 17633: }
178 :
179 : /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
180 : /// the width, suffix, and signedness of the given type
181 : static void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty,
182 15114: const TargetInfo &TI, MacroBuilder &Builder) {
183 : DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
184 15114: TI.isTypeSigned(Ty), Builder);
185 15114: }
186 :
187 : static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty,
188 25183: MacroBuilder &Builder) {
189 25183: Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
190 25183: }
191 :
192 : static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty,
193 17633: const TargetInfo &TI, MacroBuilder &Builder) {
194 17633: Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty)));
195 17633: }
196 :
197 : static void DefineExactWidthIntType(TargetInfo::IntType Ty,
198 7550: const TargetInfo &TI, MacroBuilder &Builder) {
199 7550: int TypeWidth = TI.getTypeWidth(Ty);
200 7550: DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder);
201 :
202 7550: llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
2519: branch 1 taken
5031: branch 2 taken
203 7550: if (!ConstSuffix.empty())
204 : Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__",
205 2519: ConstSuffix);
206 7550: }
207 :
208 : static void InitializePredefinedMacros(const TargetInfo &TI,
209 : const LangOptions &LangOpts,
210 : const FrontendOptions &FEOpts,
211 2519: MacroBuilder &Builder) {
212 : // Compiler version introspection macros.
213 2519: Builder.defineMacro("__llvm__"); // LLVM Backend
214 2519: Builder.defineMacro("__clang__"); // Clang Frontend
215 :
216 : // Currently claim to be compatible with GCC 4.2.1-5621.
217 2519: Builder.defineMacro("__GNUC_MINOR__", "2");
218 2519: Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
219 2519: Builder.defineMacro("__GNUC__", "4");
220 2519: Builder.defineMacro("__GXX_ABI_VERSION", "1002");
221 2519: Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible Clang Compiler\"");
222 :
223 : // Initialize language-specific preprocessor defines.
224 :
225 : // These should all be defined in the preprocessor according to the
226 : // current language configuration.
2497: branch 0 taken
22: branch 1 taken
227 2519: if (!LangOpts.Microsoft)
228 2497: Builder.defineMacro("__STDC__");
4: branch 0 taken
2515: branch 1 taken
229 2519: if (LangOpts.AsmPreprocessor)
230 4: Builder.defineMacro("__ASSEMBLER__");
231 :
1626: branch 0 taken
893: branch 1 taken
232 2519: if (!LangOpts.CPlusPlus) {
1598: branch 0 taken
28: branch 1 taken
233 1626: if (LangOpts.C99)
234 1598: Builder.defineMacro("__STDC_VERSION__", "199901L");
23: branch 0 taken
5: branch 1 taken
1: branch 2 taken
22: branch 3 taken
235 28: else if (!LangOpts.GNUMode && LangOpts.Digraphs)
236 1: Builder.defineMacro("__STDC_VERSION__", "199409L");
237 : }
238 :
239 : // Standard conforming mode?
151: branch 0 taken
2368: branch 1 taken
240 2519: if (!LangOpts.GNUMode)
241 151: Builder.defineMacro("__STRICT_ANSI__");
242 :
100: branch 0 taken
2419: branch 1 taken
243 2519: if (LangOpts.CPlusPlus0x)
244 100: Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
245 :
29: branch 0 taken
2490: branch 1 taken
246 2519: if (LangOpts.Freestanding)
247 29: Builder.defineMacro("__STDC_HOSTED__", "0");
248 : else
249 2490: Builder.defineMacro("__STDC_HOSTED__");
250 :
645: branch 0 taken
1874: branch 1 taken
251 2519: if (LangOpts.ObjC1) {
252 645: Builder.defineMacro("__OBJC__");
33: branch 0 taken
612: branch 1 taken
253 645: if (LangOpts.ObjCNonFragileABI) {
254 33: Builder.defineMacro("__OBJC2__");
255 33: Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
256 : }
257 :
48: branch 1 taken
597: branch 2 taken
258 645: if (LangOpts.getGCMode() != LangOptions::NonGC)
259 48: Builder.defineMacro("__OBJC_GC__");
260 :
588: branch 0 taken
57: branch 1 taken
261 645: if (LangOpts.NeXTRuntime)
262 588: Builder.defineMacro("__NEXT_RUNTIME__");
263 : }
264 :
265 : // darwin_constant_cfstrings controls this. This is also dependent
266 : // on other things like the runtime I believe. This is set even for C code.
267 2519: Builder.defineMacro("__CONSTANT_CFSTRINGS__");
268 :
645: branch 0 taken
1874: branch 1 taken
269 2519: if (LangOpts.ObjC2)
270 645: Builder.defineMacro("OBJC_NEW_PROPERTIES");
271 :
3: branch 0 taken
2516: branch 1 taken
272 2519: if (LangOpts.PascalStrings)
273 3: Builder.defineMacro("__PASCAL_STRINGS__");
274 :
129: branch 0 taken
2390: branch 1 taken
275 2519: if (LangOpts.Blocks) {
276 129: Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
277 129: Builder.defineMacro("__BLOCKS__");
278 : }
279 :
17: branch 0 taken
2502: branch 1 taken
280 2519: if (LangOpts.Exceptions)
281 17: Builder.defineMacro("__EXCEPTIONS");
282 :
893: branch 0 taken
1626: branch 1 taken
283 2519: if (LangOpts.CPlusPlus) {
284 893: Builder.defineMacro("__DEPRECATED");
285 893: Builder.defineMacro("__GNUG__", "4");
286 893: Builder.defineMacro("__GXX_WEAK__");
781: branch 0 taken
112: branch 1 taken
287 893: if (LangOpts.GNUMode)
288 781: Builder.defineMacro("__cplusplus");
289 : else
290 : // C++ [cpp.predefined]p1:
291 : // The name_ _cplusplusis defined to the value199711Lwhen compiling a
292 : // C++ translation unit.
293 112: Builder.defineMacro("__cplusplus", "199711L");
294 893: Builder.defineMacro("__private_extern__", "extern");
295 : // Ugly hack to work with GNU libstdc++.
296 893: Builder.defineMacro("_GNU_SOURCE");
297 : }
298 :
22: branch 0 taken
2497: branch 1 taken
299 2519: if (LangOpts.Microsoft) {
300 : // Filter out some microsoft extensions when trying to parse in ms-compat
301 : // mode.
302 22: Builder.defineMacro("__int8", "__INT8_TYPE__");
303 22: Builder.defineMacro("__int16", "__INT16_TYPE__");
304 22: Builder.defineMacro("__int32", "__INT32_TYPE__");
305 22: Builder.defineMacro("__int64", "__INT64_TYPE__");
306 : // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
307 : // VC++ appears to only like __FUNCTION__.
308 22: Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
309 : // Work around some issues with Visual C++ headerws.
13: branch 0 taken
9: branch 1 taken
310 22: if (LangOpts.CPlusPlus) {
311 : // Since we define wchar_t in C++ mode.
312 13: Builder.defineMacro("_WCHAR_T_DEFINED");
313 13: Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
314 : // FIXME: This should be temporary until we have a __pragma
315 : // solution, to avoid some errors flagged in VC++ headers.
316 13: Builder.defineMacro("_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES", "0");
317 : }
318 : }
319 :
33: branch 0 taken
2486: branch 1 taken
320 2519: if (LangOpts.Optimize)
321 33: Builder.defineMacro("__OPTIMIZE__");
0: branch 0 not taken
2519: branch 1 taken
322 2519: if (LangOpts.OptimizeSize)
323 0: Builder.defineMacro("__OPTIMIZE_SIZE__");
324 :
325 : // Initialize target-specific preprocessor defines.
326 :
327 : // Define type sizing macros based on the target properties.
2519: branch 1 taken
0: branch 2 not taken
328 2519: assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
329 2519: Builder.defineMacro("__CHAR_BIT__", "8");
330 :
331 2519: DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
332 2519: DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
333 2519: DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
334 2519: DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
335 2519: DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
336 2519: DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
337 2519: DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
338 :
339 2519: DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
340 2519: DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
341 2519: DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
342 2519: DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
343 2519: DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
344 2519: DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
345 2519: DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
346 2519: DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
347 2519: DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
348 2519: DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
349 2519: DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
350 2519: DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
351 2519: DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
352 2519: DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
353 :
354 2519: DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
355 2519: DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
356 2519: DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
357 :
358 : // Define a __POINTER_WIDTH__ macro for stdint.h.
359 : Builder.defineMacro("__POINTER_WIDTH__",
360 2519: llvm::Twine((int)TI.getPointerWidth(0)));
361 :
3: branch 0 taken
2516: branch 1 taken
362 2519: if (!LangOpts.CharIsSigned)
363 3: Builder.defineMacro("__CHAR_UNSIGNED__");
364 :
365 : // Define exact-width integer types for stdint.h
366 : Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__",
367 2519: "char");
368 :
2519: branch 2 taken
0: branch 3 not taken
369 2519: if (TI.getShortWidth() > TI.getCharWidth())
370 2519: DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
371 :
2512: branch 2 taken
7: branch 3 taken
372 2519: if (TI.getIntWidth() > TI.getShortWidth())
373 2512: DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
374 :
209: branch 2 taken
2310: branch 3 taken
375 2519: if (TI.getLongWidth() > TI.getIntWidth())
376 209: DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
377 :
2310: branch 2 taken
209: branch 3 taken
378 2519: if (TI.getLongLongWidth() > TI.getLongWidth())
379 2310: DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
380 :
381 : // Add __builtin_va_list typedef.
382 2519: Builder.append(TI.getVAListDeclaration());
383 :
2519: branch 1 taken
0: branch 2 not taken
384 2519: if (const char *Prefix = TI.getUserLabelPrefix())
385 2519: Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
386 :
387 : // Build configuration options. FIXME: these should be controlled by
388 : // command line options or something.
389 2519: Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
390 :
921: branch 0 taken
1598: branch 1 taken
391 2519: if (LangOpts.GNUInline)
392 921: Builder.defineMacro("__GNUC_GNU_INLINE__");
393 : else
394 1598: Builder.defineMacro("__GNUC_STDC_INLINE__");
395 :
2486: branch 0 taken
33: branch 1 taken
396 2519: if (LangOpts.NoInline)
397 2486: Builder.defineMacro("__NO_INLINE__");
398 :
17: branch 0 taken
2502: branch 1 taken
399 2519: if (unsigned PICLevel = LangOpts.PICLevel) {
400 17: Builder.defineMacro("__PIC__", llvm::Twine(PICLevel));
401 17: Builder.defineMacro("__pic__", llvm::Twine(PICLevel));
402 : }
403 :
404 : // Macros to control C99 numerics and <float.h>
405 2519: Builder.defineMacro("__FLT_EVAL_METHOD__", "0");
406 2519: Builder.defineMacro("__FLT_RADIX__", "2");
407 2519: int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
408 2519: Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig));
409 :
4: branch 1 taken
2515: branch 2 taken
410 2519: if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
411 4: Builder.defineMacro("__SSP__");
1: branch 1 taken
2514: branch 2 taken
412 2515: else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
413 1: Builder.defineMacro("__SSP_ALL__", "2");
414 :
51: branch 0 taken
2468: branch 1 taken
415 2519: if (FEOpts.ProgramAction == frontend::RewriteObjC)
416 51: Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
417 : // Get other target #defines.
418 2519: TI.getTargetDefines(LangOpts, Builder);
419 2519: }
420 :
421 : // Initialize the remapping of files to alternative contents, e.g.,
422 : // those specified through other files.
423 : static void InitializeFileRemapping(Diagnostic &Diags,
424 : SourceManager &SourceMgr,
425 : FileManager &FileMgr,
426 2520: const PreprocessorOptions &InitOpts) {
427 : // Remap files in the source manager (with buffers).
3: branch 2 taken
2520: branch 3 taken
428 2523: for (PreprocessorOptions::remapped_file_buffer_iterator
429 2520: Remap = InitOpts.remapped_file_buffer_begin(),
430 2520: RemapEnd = InitOpts.remapped_file_buffer_end();
431 : Remap != RemapEnd;
432 : ++Remap) {
433 : // Create the file entry for the file that we're mapping from.
434 : const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
435 : Remap->second->getBufferSize(),
436 3: 0);
0: branch 0 not taken
3: branch 1 taken
437 3: if (!FromFile) {
438 : Diags.Report(diag::err_fe_remap_missing_from_file)
439 0: << Remap->first;
440 0: continue;
441 : }
442 :
443 : // Override the contents of the "from" file with the contents of
444 : // the "to" file.
445 3: SourceMgr.overrideFileContents(FromFile, Remap->second);
446 : }
447 :
448 : // Remap files in the source manager (with other files).
6: branch 1 taken
0: branch 2 not taken
6: branch 5 taken
2520: branch 6 taken
449 2532: for (PreprocessorOptions::remapped_file_iterator
450 2520: Remap = InitOpts.remapped_file_begin(),
451 2520: RemapEnd = InitOpts.remapped_file_end();
452 : Remap != RemapEnd;
453 : ++Remap) {
454 : // Find the file that we're mapping to.
455 6: const FileEntry *ToFile = FileMgr.getFile(Remap->second);
0: branch 0 not taken
6: branch 1 taken
456 6: if (!ToFile) {
457 : Diags.Report(diag::err_fe_remap_missing_to_file)
458 0: << Remap->first << Remap->second;
459 0: continue;
460 : }
461 :
462 : // Create the file entry for the file that we're mapping from.
463 : const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
464 : ToFile->getSize(),
465 6: 0);
0: branch 0 not taken
6: branch 1 taken
466 6: if (!FromFile) {
467 : Diags.Report(diag::err_fe_remap_missing_from_file)
468 0: << Remap->first;
469 0: continue;
470 : }
471 :
472 : // Load the contents of the file we're mapping to.
473 6: std::string ErrorStr;
474 : const llvm::MemoryBuffer *Buffer
475 6: = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
0: branch 0 not taken
6: branch 1 taken
476 6: if (!Buffer) {
477 : Diags.Report(diag::err_fe_error_opening)
478 0: << Remap->second << ErrorStr;
479 0: continue;
480 : }
481 :
482 : // Override the contents of the "from" file with the contents of
483 : // the "to" file.
484 6: SourceMgr.overrideFileContents(FromFile, Buffer);
485 : }
486 2520: }
487 :
488 : /// InitializePreprocessor - Initialize the preprocessor getting it and the
489 : /// environment ready to process a single file. This returns true on error.
490 : ///
491 : void clang::InitializePreprocessor(Preprocessor &PP,
492 : const PreprocessorOptions &InitOpts,
493 : const HeaderSearchOptions &HSOpts,
494 2520: const FrontendOptions &FEOpts) {
495 2520: std::string PredefineBuffer;
496 2520: PredefineBuffer.reserve(4080);
497 2520: llvm::raw_string_ostream Predefines(PredefineBuffer);
498 2520: MacroBuilder Builder(Predefines);
499 :
500 : InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
501 2520: PP.getFileManager(), InitOpts);
502 :
503 2520: Builder.append("# 1 \"<built-in>\" 3");
504 :
505 : // Install things like __POWERPC__, __GNUC__, etc into the macro table.
2519: branch 0 taken
1: branch 1 taken
506 2520: if (InitOpts.UsePredefines)
507 : InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
508 2519: FEOpts, Builder);
509 :
510 : // Add on the predefines from the driver. Wrap in a #line directive to report
511 : // that they come from the command line.
512 2520: Builder.append("# 1 \"<command line>\" 1");
513 :
514 : // Process #define's and #undef's in the order they are given.
78: branch 1 taken
2520: branch 2 taken
515 2598: for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
0: branch 1 not taken
78: branch 2 taken
516 78: if (InitOpts.Macros[i].second) // isUndef
517 0: Builder.undefineMacro(InitOpts.Macros[i].first);
518 : else
519 : DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
520 78: PP.getDiagnostics());
521 : }
522 :
523 : // If -imacros are specified, include them now. These are processed before
524 : // any -include directives.
1: branch 1 taken
2520: branch 2 taken
525 2521: for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
526 1: AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
527 :
528 : // Process -include directives.
65: branch 1 taken
2520: branch 2 taken
529 2585: for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
530 65: const std::string &Path = InitOpts.Includes[i];
1: branch 1 taken
64: branch 2 taken
531 65: if (Path == InitOpts.ImplicitPTHInclude)
532 1: AddImplicitIncludePTH(Builder, PP, Path);
533 : else
534 64: AddImplicitInclude(Builder, Path);
535 : }
536 :
537 : // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
538 2520: Builder.append("# 1 \"<built-in>\" 2");
539 :
540 : // Copy PredefinedBuffer into the Preprocessor.
541 2520: PP.setPredefines(Predefines.str());
542 :
543 : // Initialize the header search object.
544 : ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
545 : PP.getLangOptions(),
546 2520: PP.getTargetInfo().getTriple());
547 2520: }
Generated: 2010-02-10 01:31 by zcov