 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
73.5% |
366 / 498 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
91.6% |
456 / 498 |
| |
|
Line Coverage: |
80.5% |
569 / 707 |
| |
 |
|
 |
1 : //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
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 # directive processing for the Preprocessor.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "clang/Lex/Preprocessor.h"
15 : #include "clang/Lex/LiteralSupport.h"
16 : #include "clang/Lex/HeaderSearch.h"
17 : #include "clang/Lex/MacroInfo.h"
18 : #include "clang/Lex/LexDiagnostic.h"
19 : #include "clang/Basic/FileManager.h"
20 : #include "clang/Basic/SourceManager.h"
21 : #include "llvm/ADT/APInt.h"
22 : using namespace clang;
23 :
24 : //===----------------------------------------------------------------------===//
25 : // Utility Methods for Preprocessor Directive Handling.
26 : //===----------------------------------------------------------------------===//
27 :
28 303204: MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
29 : MacroInfo *MI;
30 :
744: branch 1 taken
302460: branch 2 taken
31 303204: if (!MICache.empty()) {
32 744: MI = MICache.back();
33 744: MICache.pop_back();
34 : } else
35 302460: MI = (MacroInfo*) BP.Allocate<MacroInfo>();
303204: branch 1 taken
0: branch 2 not taken
36 303204: new (MI) MacroInfo(L);
37 303204: return MI;
38 : }
39 :
40 : /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will
41 : /// be reused for allocating new MacroInfo objects.
42 753: void Preprocessor::ReleaseMacroInfo(MacroInfo* MI) {
43 753: MICache.push_back(MI);
44 753: MI->FreeArgumentList(BP);
45 753: }
46 :
47 :
48 : /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
49 : /// current line until the tok::eom token is found.
50 3107: void Preprocessor::DiscardUntilEndOfDirective() {
51 3107: Token Tmp;
3836: branch 1 taken
3107: branch 2 taken
52 6943: do {
53 6943: LexUnexpandedToken(Tmp);
54 : } while (Tmp.isNot(tok::eom));
55 3107: }
56 :
57 : /// ReadMacroName - Lex and validate a macro name, which occurs after a
58 : /// #define or #undef. This sets the token kind to eom and discards the rest
59 : /// of the macro line if the macro name is invalid. isDefineUndef is 1 if
60 : /// this is due to a a #define, 2 if #undef directive, 0 if it is something
61 : /// else (e.g. #ifdef).
62 273587: void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
63 : // Read the token, don't allow macro expansion on it.
64 273587: LexUnexpandedToken(MacroNameTok);
65 :
66 : // Missing macro name?
1: branch 1 taken
273586: branch 2 taken
67 273587: if (MacroNameTok.is(tok::eom)) {
68 1: Diag(MacroNameTok, diag::err_pp_missing_macro_name);
69 1: return;
70 : }
71 :
72 273586: IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1: branch 0 taken
273585: branch 1 taken
73 273586: if (II == 0) {
74 1: std::string Spelling = getSpelling(MacroNameTok);
75 1: const IdentifierInfo &Info = Identifiers.get(Spelling);
1: branch 1 taken
0: branch 2 not taken
76 1: if (Info.isCPlusPlusOperatorKeyword())
77 : // C++ 2.5p2: Alternative tokens behave the same as its primary token
78 : // except for their spellings.
79 1: Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling;
80 : else
81 0: Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
82 : // Fall through on error.
270990: branch 0 taken
2595: branch 1 taken
0: branch 3 not taken
270990: branch 4 taken
0: branch 5 not taken
273585: branch 6 taken
83 273585: } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
84 : // Error if defining "defined": C99 6.10.8.4.
85 0: Diag(MacroNameTok, diag::err_defined_macro_name);
270990: branch 0 taken
2595: branch 1 taken
743: branch 3 taken
270247: branch 4 taken
0: branch 7 not taken
743: branch 8 taken
273585: branch 9 taken
0: branch 10 not taken
86 273585: } else if (isDefineUndef && II->hasMacroDefinition() &&
87 : getMacroInfo(II)->isBuiltinMacro()) {
88 : // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
0: branch 0 not taken
0: branch 1 not taken
89 0: if (isDefineUndef == 1)
90 0: Diag(MacroNameTok, diag::pp_redef_builtin_macro);
91 : else
92 0: Diag(MacroNameTok, diag::pp_undef_builtin_macro);
93 : } else {
94 : // Okay, we got a good identifier node. Return it.
95 273585: return;
96 : }
97 :
98 : // Invalid macro name, read and discard the rest of the line. Then set the
99 : // token kind to tok::eom.
100 1: MacroNameTok.setKind(tok::eom);
101 1: return DiscardUntilEndOfDirective();
102 : }
103 :
104 : /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
105 : /// not, emit a diagnostic and consume up until the eom. If EnableMacros is
106 : /// true, then we consider macros that expand to zero tokens as being ok.
107 11533: void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) {
108 11533: Token Tmp;
109 : // Lex unexpanded tokens for most directives: macros might expand to zero
110 : // tokens, causing us to miss diagnosing invalid lines. Some directives (like
111 : // #line) allow empty macros.
707: branch 0 taken
10826: branch 1 taken
112 11533: if (EnableMacros)
113 707: Lex(Tmp);
114 : else
115 10826: LexUnexpandedToken(Tmp);
116 :
117 : // There should be no tokens after the directive, but we allow them as an
118 : // extension.
1: branch 1 taken
11533: branch 2 taken
119 23067: while (Tmp.is(tok::comment)) // Skip comments in -C mode.
120 1: LexUnexpandedToken(Tmp);
121 :
14: branch 1 taken
11519: branch 2 taken
122 11533: if (Tmp.isNot(tok::eom)) {
123 : // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
124 : // because it is more trouble than it is worth to insert /**/ and check that
125 : // there is no /**/ in the range also.
126 14: CodeModificationHint FixItHint;
0: branch 0 not taken
14: branch 1 taken
14: branch 2 taken
14: branch 3 taken
14: branch 4 taken
14: branch 5 taken
127 14: if (Features.GNUMode || Features.C99 || Features.CPlusPlus)
128 14: FixItHint = CodeModificationHint::CreateInsertion(Tmp.getLocation(),"//");
129 14: Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << FixItHint;
130 14: DiscardUntilEndOfDirective();
131 : }
132 11533: }
133 :
134 :
135 :
136 : /// SkipExcludedConditionalBlock - We just read a #if or related directive and
137 : /// decided that the subsequent tokens are in the #if'd out portion of the
138 : /// file. Lex the rest of the file, until we see an #endif. If
139 : /// FoundNonSkipPortion is true, then we have already emitted code for part of
140 : /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
141 : /// is true, then #else directives are ok, if not, then we have already seen one
142 : /// so a #else directive is a duplicate. When this returns, the caller can lex
143 : /// the first valid token.
144 : void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
145 : bool FoundNonSkipPortion,
146 2655: bool FoundElse) {
147 2655: ++NumSkipped;
2655: branch 1 taken
0: branch 2 not taken
2655: branch 3 taken
0: branch 4 not taken
148 2655: assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?");
149 :
150 : CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
151 2655: FoundNonSkipPortion, FoundElse);
152 :
0: branch 1 not taken
2655: branch 2 taken
153 2655: if (CurPTHLexer) {
154 0: PTHSkipExcludedConditionalBlock();
155 0: return;
156 : }
157 :
158 : // Enter raw mode to disable identifier lookup (and thus macro expansion),
159 : // disabling warnings, etc.
160 2655: CurPPLexer->LexingRawMode = true;
161 2655: Token Tok;
162 114766: while (1) {
163 117421: CurLexer->Lex(Tok);
164 :
165 : // If this is the end of the buffer, we have an error.
1: branch 1 taken
117420: branch 2 taken
166 117421: if (Tok.is(tok::eof)) {
167 : // Emit errors for each unterminated conditional on the stack, including
168 : // the current one.
1: branch 1 taken
1: branch 2 taken
169 3: while (!CurPPLexer->ConditionalStack.empty()) {
170 : Diag(CurPPLexer->ConditionalStack.back().IfLoc,
171 1: diag::err_pp_unterminated_conditional);
172 1: CurPPLexer->ConditionalStack.pop_back();
173 : }
174 :
175 : // Just return and let the caller lex after this #include.
176 1: break;
177 : }
178 :
179 : // If this token is not a preprocessor directive, just skip it.
16928: branch 1 taken
100492: branch 2 taken
81: branch 4 taken
16847: branch 5 taken
100573: branch 6 taken
16847: branch 7 taken
180 117420: if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
181 100573: continue;
182 :
183 : // We just parsed a # character at the start of a line, so we're in
184 : // directive mode. Tell the lexer this so any newlines we see will be
185 : // converted into an EOM token (this terminates the macro).
186 16847: CurPPLexer->ParsingPreprocessorDirective = true;
16847: branch 1 taken
0: branch 2 not taken
187 16847: if (CurLexer) CurLexer->SetCommentRetentionState(false);
188 :
189 :
190 : // Read the next token, the directive flavor.
191 16847: LexUnexpandedToken(Tok);
192 :
193 : // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
194 : // something bogus), skip it.
0: branch 1 not taken
16847: branch 2 taken
195 16847: if (Tok.isNot(tok::identifier)) {
196 0: CurPPLexer->ParsingPreprocessorDirective = false;
197 : // Restore comment saving mode.
0: branch 1 not taken
0: branch 2 not taken
198 0: if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
199 0: continue;
200 : }
201 :
202 : // If the first letter isn't i or e, it isn't intesting to us. We know that
203 : // this is safe in the face of spelling differences, because there is no way
204 : // to spell an i/e in a strange way that is another letter. Skipping this
205 : // allows us to avoid looking up the identifier info for #define/#undef and
206 : // other common directives.
207 16847: const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
208 16847: char FirstChar = RawCharData[0];
16846: branch 0 taken
1: branch 1 taken
16846: branch 2 taken
0: branch 3 not taken
14895: branch 4 taken
1951: branch 5 taken
9765: branch 6 taken
5130: branch 7 taken
209 16847: if (FirstChar >= 'a' && FirstChar <= 'z' &&
210 : FirstChar != 'i' && FirstChar != 'e') {
211 9765: CurPPLexer->ParsingPreprocessorDirective = false;
212 : // Restore comment saving mode.
9765: branch 1 taken
0: branch 2 not taken
213 9765: if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
214 9765: continue;
215 : }
216 :
217 : // Get the identifier name without trigraphs or embedded newlines. Note
218 : // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
219 : // when skipping.
220 : char DirectiveBuf[20];
221 7082: llvm::StringRef Directive;
7081: branch 1 taken
1: branch 2 taken
7081: branch 4 taken
0: branch 5 not taken
7081: branch 6 taken
1: branch 7 taken
222 7082: if (!Tok.needsCleaning() && Tok.getLength() < 20) {
223 7081: Directive = llvm::StringRef(RawCharData, Tok.getLength());
224 : } else {
225 1: std::string DirectiveStr = getSpelling(Tok);
226 1: unsigned IdLen = DirectiveStr.size();
0: branch 0 not taken
1: branch 1 taken
227 1: if (IdLen >= 20) {
228 0: CurPPLexer->ParsingPreprocessorDirective = false;
229 : // Restore comment saving mode.
0: branch 1 not taken
0: branch 2 not taken
230 0: if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
231 0: continue;
232 : }
233 1: memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
1: branch 2 taken
0: branch 3 not taken
234 1: Directive = llvm::StringRef(DirectiveBuf, IdLen);
235 : }
236 :
1679: branch 2 taken
5403: branch 3 taken
237 7082: if (Directive.startswith("if")) {
238 1679: llvm::StringRef Sub = Directive.substr(2);
1313: branch 1 taken
366: branch 2 taken
238: branch 5 taken
1075: branch 6 taken
238: branch 9 taken
0: branch 10 not taken
1679: branch 11 taken
0: branch 12 not taken
239 1679: if (Sub.empty() || // "if"
240 : Sub == "def" || // "ifdef"
241 : Sub == "ndef") { // "ifndef"
242 : // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
243 : // bother parsing the condition.
244 1679: DiscardUntilEndOfDirective();
245 : CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
246 : /*foundnonskip*/false,
247 1679: /*fnddelse*/false);
248 : }
5131: branch 1 taken
272: branch 2 taken
249 5403: } else if (Directive[0] == 'e') {
250 5131: llvm::StringRef Sub = Directive.substr(1);
3608: branch 2 taken
1523: branch 3 taken
251 5131: if (Sub == "ndif") { // "endif"
252 3608: CheckEndOfDirective("endif");
253 3608: PPConditionalInfo CondInfo;
254 3608: CondInfo.WasSkipping = true; // Silence bogus warning.
255 3608: bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
256 3608: InCond = InCond; // Silence warning in no-asserts mode.
0: branch 0 not taken
3608: branch 1 taken
257 3608: assert(!InCond && "Can't be skipping if not in a conditional!");
258 :
259 : // If we popped the outermost skipping block, we're done skipping!
1679: branch 0 taken
1929: branch 1 taken
260 3608: if (!CondInfo.WasSkipping)
261 1929: break;
1255: branch 2 taken
268: branch 3 taken
262 1523: } else if (Sub == "lse") { // "else".
263 : // #else directive in a skipping conditional. If not in some other
264 : // skipping conditional, and if #else hasn't already been seen, enter it
265 : // as a non-skipping conditional.
266 1255: DiscardUntilEndOfDirective(); // C99 6.10p4.
267 1255: PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
268 :
269 : // If this is a #else with a #else before it, report the error.
0: branch 0 not taken
1255: branch 1 taken
270 1255: if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
271 :
272 : // Note that we've seen a #else in this conditional.
273 1255: CondInfo.FoundElse = true;
274 :
275 : // If the conditional is at the top level, and the #if block wasn't
276 : // entered, enter the #else block now.
741: branch 0 taken
514: branch 1 taken
704: branch 2 taken
37: branch 3 taken
277 1255: if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
278 704: CondInfo.FoundNonSkip = true;
279 704: break;
280 : }
61: branch 2 taken
207: branch 3 taken
281 268: } else if (Sub == "lif") { // "elif".
282 61: PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
283 :
284 : bool ShouldEnter;
285 : // If this is in a skipping block or if we're already handled this #if
286 : // block, don't bother parsing the condition.
44: branch 0 taken
17: branch 1 taken
0: branch 2 not taken
44: branch 3 taken
287 78: if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
288 17: DiscardUntilEndOfDirective();
289 17: ShouldEnter = false;
290 : } else {
291 : // Restore the value of LexingRawMode so that identifiers are
292 : // looked up, etc, inside the #elif expression.
0: branch 0 not taken
44: branch 1 taken
293 44: assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
294 44: CurPPLexer->LexingRawMode = false;
295 44: IdentifierInfo *IfNDefMacro = 0;
296 44: ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
297 44: CurPPLexer->LexingRawMode = true;
298 : }
299 :
300 : // If this is a #elif with a #else before it, report the error.
0: branch 0 not taken
61: branch 1 taken
301 61: if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
302 :
303 : // If this condition is true, enter it!
21: branch 0 taken
40: branch 1 taken
304 61: if (ShouldEnter) {
305 21: CondInfo.FoundNonSkip = true;
306 21: break;
307 : }
308 : }
309 : }
310 :
311 4428: CurPPLexer->ParsingPreprocessorDirective = false;
312 : // Restore comment saving mode.
4428: branch 1 taken
0: branch 2 not taken
313 4428: if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments);
314 : }
315 :
316 : // Finally, if we are out of the conditional (saw an #endif or ran off the end
317 : // of the file, just stop skipping and return to lexing whatever came after
318 : // the #if block.
319 2655: CurPPLexer->LexingRawMode = false;
320 : }
321 :
322 0: void Preprocessor::PTHSkipExcludedConditionalBlock() {
323 :
324 0: while (1) {
0: branch 1 not taken
0: branch 2 not taken
325 0: assert(CurPTHLexer);
0: branch 1 not taken
0: branch 2 not taken
326 0: assert(CurPTHLexer->LexingRawMode == false);
327 :
328 : // Skip to the next '#else', '#elif', or #endif.
0: branch 2 not taken
0: branch 3 not taken
329 0: if (CurPTHLexer->SkipBlock()) {
330 : // We have reached an #endif. Both the '#' and 'endif' tokens
331 : // have been consumed by the PTHLexer. Just pop off the condition level.
332 0: PPConditionalInfo CondInfo;
333 0: bool InCond = CurPTHLexer->popConditionalLevel(CondInfo);
334 0: InCond = InCond; // Silence warning in no-asserts mode.
0: branch 0 not taken
0: branch 1 not taken
335 0: assert(!InCond && "Can't be skipping if not in a conditional!");
336 0: break;
337 : }
338 :
339 : // We have reached a '#else' or '#elif'. Lex the next token to get
340 : // the directive flavor.
341 0: Token Tok;
342 0: LexUnexpandedToken(Tok);
343 :
344 : // We can actually look up the IdentifierInfo here since we aren't in
345 : // raw mode.
346 0: tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID();
347 :
0: branch 0 not taken
0: branch 1 not taken
348 0: if (K == tok::pp_else) {
349 : // #else: Enter the else condition. We aren't in a nested condition
350 : // since we skip those. We're always in the one matching the last
351 : // blocked we skipped.
352 0: PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
353 : // Note that we've seen a #else in this conditional.
354 0: CondInfo.FoundElse = true;
355 :
356 : // If the #if block wasn't entered then enter the #else block now.
0: branch 0 not taken
0: branch 1 not taken
357 0: if (!CondInfo.FoundNonSkip) {
358 0: CondInfo.FoundNonSkip = true;
359 :
360 : // Scan until the eom token.
361 0: CurPTHLexer->ParsingPreprocessorDirective = true;
362 0: DiscardUntilEndOfDirective();
363 0: CurPTHLexer->ParsingPreprocessorDirective = false;
364 :
365 0: break;
366 : }
367 :
368 : // Otherwise skip this block.
369 0: continue;
370 : }
371 :
0: branch 0 not taken
0: branch 1 not taken
372 0: assert(K == tok::pp_elif);
373 0: PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel();
374 :
375 : // If this is a #elif with a #else before it, report the error.
0: branch 0 not taken
0: branch 1 not taken
376 0: if (CondInfo.FoundElse)
377 0: Diag(Tok, diag::pp_err_elif_after_else);
378 :
379 : // If this is in a skipping block or if we're already handled this #if
380 : // block, don't bother parsing the condition. We just skip this block.
0: branch 0 not taken
0: branch 1 not taken
381 0: if (CondInfo.FoundNonSkip)
382 0: continue;
383 :
384 : // Evaluate the condition of the #elif.
385 0: IdentifierInfo *IfNDefMacro = 0;
386 0: CurPTHLexer->ParsingPreprocessorDirective = true;
387 0: bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
388 0: CurPTHLexer->ParsingPreprocessorDirective = false;
389 :
390 : // If this condition is true, enter it!
0: branch 0 not taken
0: branch 1 not taken
391 0: if (ShouldEnter) {
392 0: CondInfo.FoundNonSkip = true;
393 0: break;
394 : }
395 :
396 : // Otherwise, skip this block and go to the next one.
397 : continue;
398 : }
399 0: }
400 :
401 : /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
402 : /// return null on failure. isAngled indicates whether the file reference is
403 : /// for system #include's or not (i.e. using <> instead of "").
404 : const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename,
405 : bool isAngled,
406 : const DirectoryLookup *FromDir,
407 727: const DirectoryLookup *&CurDir) {
408 : // If the header lookup mechanism may be relative to the current file, pass in
409 : // info about where the current file is.
410 727: const FileEntry *CurFileEnt = 0;
684: branch 0 taken
43: branch 1 taken
411 727: if (!FromDir) {
412 684: FileID FID = getCurrentFileLexer()->getFileID();
413 684: CurFileEnt = SourceMgr.getFileEntryForID(FID);
414 :
415 : // If there is no file entry associated with this file, it must be the
416 : // predefines buffer. Any other file is not lexed with a normal lexer, so
417 : // it won't be scanned for preprocessor directives. If we have the
418 : // predefines buffer, resolve #include references (which come from the
419 : // -include command line argument) as if they came from the main file, this
420 : // affects file lookup etc.
32: branch 0 taken
652: branch 1 taken
421 684: if (CurFileEnt == 0) {
422 32: FID = SourceMgr.getMainFileID();
423 32: CurFileEnt = SourceMgr.getFileEntryForID(FID);
424 : }
425 : }
426 :
427 : // Do a standard file entry lookup.
428 727: CurDir = CurDirLookup;
429 : const FileEntry *FE =
430 727: HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt);
720: branch 0 taken
7: branch 1 taken
431 727: if (FE) return FE;
432 :
433 : // Otherwise, see if this is a subframework header. If so, this is relative
434 : // to one of the headers on the #include stack. Walk the list of the current
435 : // headers on the #include stack and pass them to HeaderInfo.
4: branch 1 taken
3: branch 2 taken
436 7: if (IsFileLexer()) {
4: branch 2 taken
0: branch 3 not taken
437 4: if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
0: branch 1 not taken
4: branch 2 taken
438 4: if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
439 0: return FE;
440 : }
441 :
5: branch 1 taken
7: branch 2 taken
442 12: for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
443 5: IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
3: branch 1 taken
2: branch 2 taken
444 5: if (IsFileLexer(ISEntry)) {
3: branch 2 taken
0: branch 3 not taken
445 3: if ((CurFileEnt =
446 : SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
0: branch 1 not taken
3: branch 2 taken
447 3: if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt)))
448 0: return FE;
449 : }
450 : }
451 :
452 : // Otherwise, we really couldn't find the file.
453 7: return 0;
454 : }
455 :
456 :
457 : //===----------------------------------------------------------------------===//
458 : // Preprocessor Directive Handling.
459 : //===----------------------------------------------------------------------===//
460 :
461 : /// HandleDirective - This callback is invoked when the lexer sees a # token
462 : /// at the start of a line. This consumes the directive, modifies the
463 : /// lexer/preprocessor state, and advances the lexer(s) so that the next token
464 : /// read is the correct one.
465 288096: void Preprocessor::HandleDirective(Token &Result) {
466 : // FIXME: Traditional: # with whitespace before it not recognized by K&R?
467 :
468 : // We just parsed a # character at the start of a line, so we're in directive
469 : // mode. Tell the lexer this so any newlines we see will be converted into an
470 : // EOM token (which terminates the directive).
471 288096: CurPPLexer->ParsingPreprocessorDirective = true;
472 :
473 288096: ++NumDirectives;
474 :
475 : // We are about to read a token. For the multiple-include optimization FA to
476 : // work, we have to remember if we had read any tokens *before* this
477 : // pp-directive.
478 288096: bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
479 :
480 : // Save the '#' token in case we need to return it later.
481 288096: Token SavedHash = Result;
482 :
483 : // Read the next token, the directive flavor. This isn't expanded due to
484 : // C99 6.10.3p8.
485 288096: LexUnexpandedToken(Result);
486 :
487 : // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
488 : // #define A(x) #x
489 : // A(abc
490 : // #warning blah
491 : // def)
492 : // If so, the user is relying on non-portable behavior, emit a diagnostic.
6: branch 0 taken
288090: branch 1 taken
493 288096: if (InMacroArgs)
494 6: Diag(Result, diag::ext_embedded_directive);
495 :
496 288096: TryAgain:
0: branch 1 not taken
0: branch 2 not taken
7620: branch 3 taken
280476: branch 4 taken
497 288096: switch (Result.getKind()) {
498 : case tok::eom:
499 0: return; // null directive.
500 : case tok::comment:
501 : // Handle stuff like "# /*foo*/ define X" in -E -C mode.
502 0: LexUnexpandedToken(Result);
503 0: goto TryAgain;
504 :
505 : case tok::numeric_constant: // # 7 GNU line marker directive.
7606: branch 1 taken
14: branch 2 taken
506 7620: if (getLangOptions().AsmPreprocessor)
507 14: break; // # 4 is not a preprocessor directive in .S files.
508 7606: return HandleDigitDirective(Result);
509 : default:
510 280476: IdentifierInfo *II = Result.getIdentifierInfo();
280473: branch 0 taken
3: branch 1 taken
511 280476: if (II == 0) break; // Not an identifier.
512 :
513 : // Ask what the preprocessor keyword ID is.
4: branch 1 taken
2307: branch 2 taken
1542: branch 3 taken
1054: branch 4 taken
43: branch 5 taken
609: branch 6 taken
2973: branch 7 taken
667: branch 8 taken
1: branch 9 taken
269952: branch 10 taken
1039: branch 11 taken
51: branch 12 taken
6: branch 13 taken
189: branch 14 taken
4: branch 15 taken
17: branch 16 taken
15: branch 17 taken
0: branch 18 not taken
0: branch 19 not taken
0: branch 20 not taken
514 280473: switch (II->getPPKeywordID()) {
515 4: default: break;
516 : // C99 6.10.1 - Conditional Inclusion.
517 : case tok::pp_if:
518 2307: return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
519 : case tok::pp_ifdef:
520 1542: return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
521 : case tok::pp_ifndef:
522 1054: return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
523 : case tok::pp_elif:
524 43: return HandleElifDirective(Result);
525 : case tok::pp_else:
526 609: return HandleElseDirective(Result);
527 : case tok::pp_endif:
528 2973: return HandleEndifDirective(Result);
529 :
530 : // C99 6.10.2 - Source File Inclusion.
531 : case tok::pp_include:
532 667: return HandleIncludeDirective(Result); // Handle #include.
533 : case tok::pp___include_macros:
534 1: return HandleIncludeMacrosDirective(Result); // Handle -imacros.
535 :
536 : // C99 6.10.3 - Macro Replacement.
537 : case tok::pp_define:
538 269952: return HandleDefineDirective(Result);
539 : case tok::pp_undef:
540 1039: return HandleUndefDirective(Result);
541 :
542 : // C99 6.10.4 - Line Control.
543 : case tok::pp_line:
544 51: return HandleLineDirective(Result);
545 :
546 : // C99 6.10.5 - Error Directive.
547 : case tok::pp_error:
548 6: return HandleUserDiagnosticDirective(Result, false);
549 :
550 : // C99 6.10.6 - Pragma Directive.
551 : case tok::pp_pragma:
552 189: return HandlePragmaDirective();
553 :
554 : // GNU Extensions.
555 : case tok::pp_import:
556 4: return HandleImportDirective(Result);
557 : case tok::pp_include_next:
558 17: return HandleIncludeNextDirective(Result);
559 :
560 : case tok::pp_warning:
561 15: Diag(Result, diag::ext_pp_warning_directive);
562 15: return HandleUserDiagnosticDirective(Result, true);
563 : case tok::pp_ident:
564 0: return HandleIdentSCCSDirective(Result);
565 : case tok::pp_sccs:
566 0: return HandleIdentSCCSDirective(Result);
567 : case tok::pp_assert:
568 : //isExtension = true; // FIXME: implement #assert
569 : break;
570 : case tok::pp_unassert:
571 : //isExtension = true; // FIXME: implement #unassert
572 : break;
573 : }
574 : break;
575 : }
576 :
577 : // If this is a .S file, treat unknown # directives as non-preprocessor
578 : // directives. This is important because # may be a comment or introduce
579 : // various pseudo-ops. Just return the # token and push back the following
580 : // token to be lexed next time.
18: branch 1 taken
3: branch 2 taken
581 21: if (getLangOptions().AsmPreprocessor) {
36: branch 2 taken
18: branch 3 taken
582 18: Token *Toks = new Token[2];
583 : // Return the # and the token after it.
584 18: Toks[0] = SavedHash;
585 18: Toks[1] = Result;
586 : // Enter this token stream so that we re-lex the tokens. Make sure to
587 : // enable macro expansion, in case the token after the # is an identifier
588 : // that is expanded.
589 18: EnterTokenStream(Toks, 2, false, true);
590 18: return;
591 : }
592 :
593 : // If we reached here, the preprocessing token is not valid!
594 3: Diag(Result, diag::err_pp_invalid_directive);
595 :
596 : // Read the rest of the PP line.
597 3: DiscardUntilEndOfDirective();
598 :
599 : // Okay, we're done parsing the directive.
600 : }
601 :
602 : /// GetLineValue - Convert a numeric token into an unsigned value, emitting
603 : /// Diagnostic DiagID if it is invalid, and returning the value in Val.
604 : static bool GetLineValue(Token &DigitTok, unsigned &Val,
605 15381: unsigned DiagID, Preprocessor &PP) {
3: branch 1 taken
15378: branch 2 taken
606 15381: if (DigitTok.isNot(tok::numeric_constant)) {
607 3: PP.Diag(DigitTok, DiagID);
608 :
3: branch 1 taken
0: branch 2 not taken
609 3: if (DigitTok.isNot(tok::eom))
610 3: PP.DiscardUntilEndOfDirective();
611 3: return true;
612 : }
613 :
614 15378: llvm::SmallString<64> IntegerBuffer;
615 15378: IntegerBuffer.resize(DigitTok.getLength());
616 15378: const char *DigitTokBegin = &IntegerBuffer[0];
617 15378: unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin);
618 :
619 : // Verify that we have a simple digit-sequence, and compute the value. This
620 : // is always a simple digit string computed in decimal, so we do this manually
621 : // here.
622 15378: Val = 0;
15640: branch 0 taken
15372: branch 1 taken
623 31012: for (unsigned i = 0; i != ActualLength; ++i) {
6: branch 0 taken
15634: branch 1 taken
624 15640: if (!isdigit(DigitTokBegin[i])) {
625 : PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
626 6: diag::err_pp_line_digit_sequence);
627 6: PP.DiscardUntilEndOfDirective();
628 6: return true;
629 : }
630 :
631 15634: unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
0: branch 0 not taken
15634: branch 1 taken
632 15634: if (NextVal < Val) { // overflow.
633 0: PP.Diag(DigitTok, DiagID);
634 0: PP.DiscardUntilEndOfDirective();
635 0: return true;
636 : }
637 15634: Val = NextVal;
638 : }
639 :
640 : // Reject 0, this is needed both by #line numbers and flags.
6: branch 0 taken
15366: branch 1 taken
641 15372: if (Val == 0) {
642 6: PP.Diag(DigitTok, DiagID);
643 6: PP.DiscardUntilEndOfDirective();
644 6: return true;
645 : }
646 :
3: branch 0 taken
15363: branch 1 taken
647 15366: if (DigitTokBegin[0] == '0')
648 3: PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal);
649 :
650 15366: return false;
651 : }
652 :
653 : /// HandleLineDirective - Handle #line directive: C99 6.10.4. The two
654 : /// acceptable forms are:
655 : /// # line digit-sequence
656 : /// # line digit-sequence "s-char-sequence"
657 51: void Preprocessor::HandleLineDirective(Token &Tok) {
658 : // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
659 : // expanded.
660 51: Token DigitTok;
661 51: Lex(DigitTok);
662 :
663 : // Validate the number and convert it to an unsigned.
664 : unsigned LineNo;
15: branch 1 taken
36: branch 2 taken
665 51: if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
666 15: return;
667 :
668 : // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
669 : // number greater than 2147483647". C90 requires that the line # be <= 32767.
36: branch 0 taken
0: branch 1 not taken
670 36: unsigned LineLimit = Features.C99 ? 2147483648U : 32768U;
3: branch 0 taken
33: branch 1 taken
671 36: if (LineNo >= LineLimit)
672 3: Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
673 :
674 36: int FilenameID = -1;
675 36: Token StrTok;
676 36: Lex(StrTok);
677 :
678 : // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
679 : // string followed by eom.
24: branch 1 taken
12: branch 2 taken
680 36: if (StrTok.is(tok::eom))
681 : ; // ok
3: branch 1 taken
21: branch 2 taken
682 24: else if (StrTok.isNot(tok::string_literal)) {
683 3: Diag(StrTok, diag::err_pp_line_invalid_filename);
684 3: DiscardUntilEndOfDirective();
685 3: return;
686 : } else {
687 : // Parse and validate the string, converting it into a unique ID.
688 21: StringLiteralParser Literal(&StrTok, 1, *this);
0: branch 0 not taken
21: branch 1 taken
689 21: assert(!Literal.AnyWide && "Didn't allow wide strings in");
0: branch 0 not taken
21: branch 1 taken
690 21: if (Literal.hadError)
691 0: return DiscardUntilEndOfDirective();
0: branch 0 not taken
21: branch 1 taken
692 21: if (Literal.Pascal) {
693 0: Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
694 0: return DiscardUntilEndOfDirective();
695 : }
696 : FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(),
697 21: Literal.GetStringLength());
698 :
699 : // Verify that there is nothing after the string, other than EOM. Because
700 : // of C99 6.10.4p5, macros that expand to empty tokens are ok.
21: branch 2 taken
0: branch 3 not taken
701 21: CheckEndOfDirective("line", true);
702 : }
703 :
704 33: SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID);
705 :
20: branch 0 taken
13: branch 1 taken
706 33: if (Callbacks)
707 : Callbacks->FileChanged(DigitTok.getLocation(), PPCallbacks::RenameFile,
708 20: SrcMgr::C_User);
709 : }
710 :
711 : /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
712 : /// marker directive.
713 : static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
714 : bool &IsSystemHeader, bool &IsExternCHeader,
715 7594: Preprocessor &PP) {
716 : unsigned FlagVal;
717 7594: Token FlagTok;
718 7594: PP.Lex(FlagTok);
13: branch 1 taken
7581: branch 2 taken
719 7594: if (FlagTok.is(tok::eom)) return false;
0: branch 1 not taken
7581: branch 2 taken
720 7581: if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
721 0: return true;
722 :
2517: branch 0 taken
5064: branch 1 taken
723 7581: if (FlagVal == 1) {
724 2517: IsFileEntry = true;
725 :
726 2517: PP.Lex(FlagTok);
2491: branch 1 taken
26: branch 2 taken
727 2517: if (FlagTok.is(tok::eom)) return false;
0: branch 1 not taken
26: branch 2 taken
728 26: if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
729 0: return true;
2513: branch 0 taken
2551: branch 1 taken
730 5064: } else if (FlagVal == 2) {
731 2513: IsFileExit = true;
732 :
733 2513: SourceManager &SM = PP.getSourceManager();
734 : // If we are leaving the current presumed file, check to make sure the
735 : // presumed include stack isn't empty!
736 : FileID CurFileID =
737 2513: SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first;
738 2513: PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
739 :
740 : // If there is no include loc (main file) or if the include loc is in a
741 : // different physical file, then we aren't in a "1" line marker flag region.
742 2513: SourceLocation IncLoc = PLoc.getIncludeLoc();
2507: branch 1 taken
6: branch 2 taken
0: branch 5 not taken
2507: branch 6 taken
6: branch 7 taken
2507: branch 8 taken
743 2513: if (IncLoc.isInvalid() ||
744 : SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) {
745 6: PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
746 6: PP.DiscardUntilEndOfDirective();
747 6: return true;
748 : }
749 :
750 2507: PP.Lex(FlagTok);
2488: branch 1 taken
19: branch 2 taken
751 2507: if (FlagTok.is(tok::eom)) return false;
0: branch 1 not taken
19: branch 2 taken
752 19: if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
753 0: return true;
754 : }
755 :
756 : // We must have 3 if there are still flags.
6: branch 0 taken
2590: branch 1 taken
757 2596: if (FlagVal != 3) {
758 6: PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
759 6: PP.DiscardUntilEndOfDirective();
760 6: return true;
761 : }
762 :
763 2590: IsSystemHeader = true;
764 :
765 2590: PP.Lex(FlagTok);
2492: branch 1 taken
98: branch 2 taken
766 2590: if (FlagTok.is(tok::eom)) return false;
0: branch 1 not taken
98: branch 2 taken
767 98: if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
768 0: return true;
769 :
770 : // We must have 4 if there is yet another flag.
3: branch 0 taken
95: branch 1 taken
771 98: if (FlagVal != 4) {
772 3: PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
773 3: PP.DiscardUntilEndOfDirective();
774 3: return true;
775 : }
776 :
777 95: IsExternCHeader = true;
778 :
779 95: PP.Lex(FlagTok);
95: branch 1 taken
0: branch 2 not taken
780 95: if (FlagTok.is(tok::eom)) return false;
781 :
782 : // There are no more valid flags here.
783 0: PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
784 0: PP.DiscardUntilEndOfDirective();
785 0: return true;
786 : }
787 :
788 : /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
789 : /// one of the following forms:
790 : ///
791 : /// # 42
792 : /// # 42 "file" ('1' | '2')?
793 : /// # 42 "file" ('1' | '2')? '3' '4'?
794 : ///
795 7606: void Preprocessor::HandleDigitDirective(Token &DigitTok) {
796 : // Validate the number and convert it to an unsigned. GNU does not have a
797 : // line # limit other than it fit in 32-bits.
798 : unsigned LineNo;
0: branch 1 not taken
7606: branch 2 taken
799 7606: if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
800 : *this))
801 0: return;
802 :
803 7606: Token StrTok;
804 7606: Lex(StrTok);
805 :
806 7606: bool IsFileEntry = false, IsFileExit = false;
807 7606: bool IsSystemHeader = false, IsExternCHeader = false;
808 7606: int FilenameID = -1;
809 :
810 : // If the StrTok is "eom", then it wasn't present. Otherwise, it must be a
811 : // string followed by eom.
7600: branch 1 taken
6: branch 2 taken
812 7606: if (StrTok.is(tok::eom))
813 : ; // ok
6: branch 1 taken
7594: branch 2 taken
814 7600: else if (StrTok.isNot(tok::string_literal)) {
815 6: Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
816 6: return DiscardUntilEndOfDirective();
817 : } else {
818 : // Parse and validate the string, converting it into a unique ID.
819 7594: StringLiteralParser Literal(&StrTok, 1, *this);
0: branch 0 not taken
7594: branch 1 taken
820 7594: assert(!Literal.AnyWide && "Didn't allow wide strings in");
0: branch 0 not taken
7594: branch 1 taken
821 7594: if (Literal.hadError)
822 15: return DiscardUntilEndOfDirective();
0: branch 0 not taken
7594: branch 1 taken
823 7594: if (Literal.Pascal) {
824 0: Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
825 0: return DiscardUntilEndOfDirective();
826 : }
827 : FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(),
828 7594: Literal.GetStringLength());
829 :
830 : // If a filename was present, read any flags that are present.
15: branch 1 taken
7579: branch 2 taken
831 7594: if (ReadLineMarkerFlags(IsFileEntry, IsFileExit,
832 : IsSystemHeader, IsExternCHeader, *this))
7579: branch 1 taken
15: branch 2 taken
833 7594: return;
834 : }
835 :
836 : // Create a line note with this information.
837 : SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID,
838 : IsFileEntry, IsFileExit,
839 7585: IsSystemHeader, IsExternCHeader);
840 :
841 : // If the preprocessor has callbacks installed, notify them of the #line
842 : // change. This is used so that the line marker comes out in -E mode for
843 : // example.
618: branch 0 taken
6967: branch 1 taken
844 7585: if (Callbacks) {
845 618: PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
202: branch 0 taken
416: branch 1 taken
846 618: if (IsFileEntry)
847 202: Reason = PPCallbacks::EnterFile;
202: branch 0 taken
214: branch 1 taken
848 416: else if (IsFileExit)
849 202: Reason = PPCallbacks::ExitFile;
850 618: SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
2: branch 0 taken
616: branch 1 taken
851 618: if (IsExternCHeader)
852 2: FileKind = SrcMgr::C_ExternCSystem;
206: branch 0 taken
410: branch 1 taken
853 616: else if (IsSystemHeader)
854 206: FileKind = SrcMgr::C_System;
855 :
856 618: Callbacks->FileChanged(DigitTok.getLocation(), Reason, FileKind);
857 : }
858 : }
859 :
860 :
861 : /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
862 : ///
863 : void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
864 21: bool isWarning) {
865 : // PTH doesn't emit #warning or #error directives.
2: branch 1 taken
19: branch 2 taken
866 21: if (CurPTHLexer)
867 2: return CurPTHLexer->DiscardToEndOfLine();
868 :
869 : // Read the rest of the line raw. We do this because we don't want macros
870 : // to be expanded and we don't require that the tokens be valid preprocessing
871 : // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
872 : // collapse multiple consequtive white space between tokens, but this isn't
873 : // specified by the standard.
874 19: std::string Message = CurLexer->ReadToEndOfLine();
13: branch 0 taken
6: branch 1 taken
875 19: if (isWarning)
876 13: Diag(Tok, diag::pp_hash_warning) << Message;
877 : else
878 6: Diag(Tok, diag::err_pp_hash_error) << Message;
879 : }
880 :
881 : /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
882 : ///
883 0: void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
884 : // Yes, this directive is an extension.
885 0: Diag(Tok, diag::ext_pp_ident_directive);
886 :
887 : // Read the string argument.
888 0: Token StrTok;
889 0: Lex(StrTok);
890 :
891 : // If the token kind isn't a string, it's a malformed directive.
0: branch 1 not taken
0: branch 2 not taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 6 not taken
0: branch 7 not taken
892 0: if (StrTok.isNot(tok::string_literal) &&
893 : StrTok.isNot(tok::wide_string_literal)) {
894 0: Diag(StrTok, diag::err_pp_malformed_ident);
0: branch 1 not taken
0: branch 2 not taken
895 0: if (StrTok.isNot(tok::eom))
896 0: DiscardUntilEndOfDirective();
897 0: return;
898 : }
899 :
900 : // Verify that there is nothing after the string, other than EOM.
901 0: CheckEndOfDirective("ident");
902 :
0: branch 0 not taken
0: branch 1 not taken
903 0: if (Callbacks)
904 0: Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
905 : }
906 :
907 : //===----------------------------------------------------------------------===//
908 : // Preprocessor Include Directive Handling.
909 : //===----------------------------------------------------------------------===//
910 :
911 : /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
912 : /// checked and spelled filename, e.g. as an operand of #include. This returns
913 : /// true if the input filename was in <>'s or false if it were in ""'s. The
914 : /// caller is expected to provide a buffer that is large enough to hold the
915 : /// spelling of the filename, but is also expected to handle the case when
916 : /// this method decides to use a different buffer.
917 : bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
918 728: llvm::StringRef &Buffer) {
919 : // Get the text form of the filename.
728: branch 1 taken
0: branch 2 not taken
920 728: assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
921 :
922 : // Make sure the filename is <x> or "x".
923 : bool isAngled;
634: branch 1 taken
94: branch 2 taken
924 728: if (Buffer[0] == '<') {
0: branch 1 not taken
634: branch 2 taken
925 634: if (Buffer.back() != '>') {
926 0: Diag(Loc, diag::err_pp_expects_filename);
927 0: Buffer = llvm::StringRef();
928 0: return true;
929 : }
930 634: isAngled = true;
94: branch 1 taken
0: branch 2 not taken
931 94: } else if (Buffer[0] == '"') {
0: branch 1 not taken
94: branch 2 taken
932 94: if (Buffer.back() != '"') {
933 0: Diag(Loc, diag::err_pp_expects_filename);
934 0: Buffer = llvm::StringRef();
935 0: return true;
936 : }
937 94: isAngled = false;
938 : } else {
939 0: Diag(Loc, diag::err_pp_expects_filename);
940 0: Buffer = llvm::StringRef();
941 0: return true;
942 : }
943 :
944 : // Diagnose #include "" as invalid.
1: branch 1 taken
727: branch 2 taken
945 728: if (Buffer.size() <= 2) {
946 1: Diag(Loc, diag::err_pp_empty_filename);
947 1: Buffer = llvm::StringRef();
948 1: return true;
949 : }
950 :
951 : // Skip the brackets.
952 727: Buffer = Buffer.substr(1, Buffer.size()-2);
953 727: return isAngled;
954 : }
955 :
956 : /// ConcatenateIncludeName - Handle cases where the #include name is expanded
957 : /// from a macro as multiple tokens, which need to be glued together. This
958 : /// occurs for code like:
959 : /// #define FOO <a/b.h>
960 : /// #include FOO
961 : /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
962 : ///
963 : /// This code concatenates and consumes tokens up to the '>' token. It returns
964 : /// false if the > was found, otherwise it returns true if it finds and consumes
965 : /// the EOM marker.
966 : bool Preprocessor::ConcatenateIncludeName(
967 3: llvm::SmallVector<char, 128> &FilenameBuffer) {
968 3: Token CurTok;
969 :
970 3: Lex(CurTok);
12: branch 1 taken
0: branch 2 not taken
971 15: while (CurTok.isNot(tok::eom)) {
972 : // Append the spelling of this token to the buffer. If there was a space
973 : // before it, add it now.
0: branch 1 not taken
12: branch 2 taken
974 12: if (CurTok.hasLeadingSpace())
975 0: FilenameBuffer.push_back(' ');
976 :
977 : // Get the spelling of the token, directly into FilenameBuffer if possible.
978 12: unsigned PreAppendSize = FilenameBuffer.size();
979 12: FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
980 :
981 12: const char *BufPtr = &FilenameBuffer[PreAppendSize];
982 12: unsigned ActualLen = getSpelling(CurTok, BufPtr);
983 :
984 : // If the token was spelled somewhere else, copy it into FilenameBuffer.
12: branch 1 taken
0: branch 2 not taken
985 12: if (BufPtr != &FilenameBuffer[PreAppendSize])
986 12: memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
987 :
988 : // Resize FilenameBuffer to the correct size.
0: branch 1 not taken
12: branch 2 taken
989 12: if (CurTok.getLength() != ActualLen)
990 0: FilenameBuffer.resize(PreAppendSize+ActualLen);
991 :
992 : // If we found the '>' marker, return success.
3: branch 1 taken
9: branch 2 taken
993 12: if (CurTok.is(tok::greater))
994 3: return false;
995 :
996 9: Lex(CurTok);
997 : }
998 :
999 : // If we hit the eom marker, emit an error and return true so that the caller
1000 : // knows the EOM has been read.
1001 0: Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1002 0: return true;
1003 : }
1004 :
1005 : /// HandleIncludeDirective - The "#include" tokens have just been read, read the
1006 : /// file to be included from the lexer, then include it! This is a common
1007 : /// routine with functionality shared between #include, #include_next and
1008 : /// #import. LookupFrom is set when this is a #include_next directive, it
1009 : /// specifies the file to start searching from.
1010 : void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
1011 : const DirectoryLookup *LookupFrom,
1012 689: bool isImport) {
1013 :
1014 689: Token FilenameTok;
1015 689: CurPPLexer->LexIncludeFilename(FilenameTok);
1016 :
1017 : // Reserve a buffer to get the spelling.
1018 689: llvm::SmallString<128> FilenameBuffer;
1019 689: llvm::StringRef Filename;
1020 :
0: branch 1 not taken
684: branch 2 taken
3: branch 3 taken
2: branch 4 taken
1021 689: switch (FilenameTok.getKind()) {
1022 : case tok::eom:
1023 : // If the token kind is EOM, the error has already been diagnosed.
1024 121: return;
1025 :
1026 : case tok::angle_string_literal:
1027 : case tok::string_literal: {
1028 684: FilenameBuffer.resize(FilenameTok.getLength());
1029 684: const char *FilenameStart = &FilenameBuffer[0];
1030 684: unsigned Len = getSpelling(FilenameTok, FilenameStart);
1031 684: Filename = llvm::StringRef(FilenameStart, Len);
1032 684: break;
1033 : }
1034 :
1035 : case tok::less:
1036 : // This could be a <foo/bar.h> file coming from a macro expansion. In this
1037 : // case, glue the tokens together into FilenameBuffer and interpret those.
1038 3: FilenameBuffer.push_back('<');
0: branch 1 not taken
3: branch 2 taken
1039 3: if (ConcatenateIncludeName(FilenameBuffer))
1040 : return; // Found <eom> but no ">"? Diagnostic already emitted.
1041 3: Filename = FilenameBuffer.str();
1042 3: break;
1043 : default:
1044 2: Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1045 2: DiscardUntilEndOfDirective();
1046 : return;
1047 : }
1048 :
1049 : bool isAngled =
1050 687: GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
1051 : // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1052 : // error.
1: branch 1 taken
686: branch 2 taken
1053 687: if (Filename.empty()) {
1054 1: DiscardUntilEndOfDirective();
1055 : return;
1056 : }
1057 :
1058 : // Verify that there is nothing after the filename, other than EOM. Note that
1059 : // we allow macros that expand to nothing after the filename, because this
1060 : // falls into the category of "#include pp-tokens new-line" specified in
1061 : // C99 6.10.2p4.
1062 686: CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1063 :
1064 : // Check that we don't have infinite #include recursion.
0: branch 1 not taken
686: branch 2 taken
1065 686: if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
1066 0: Diag(FilenameTok, diag::err_pp_include_too_deep);
1067 : return;
1068 : }
1069 :
1070 : // Search include directories.
1071 : const DirectoryLookup *CurDir;
1072 686: const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
2: branch 0 taken
684: branch 1 taken
1073 686: if (File == 0) {
1074 2: Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1075 : return;
1076 : }
1077 :
1078 : // Ask HeaderInfo if we should enter this #include file. If not, #including
1079 : // this file will have no effect.
116: branch 1 taken
568: branch 2 taken
1080 684: if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport))
1081 : return;
1082 :
1083 : // The #included file will be considered to be a system header if either it is
1084 : // in a system include directory, or if the #includer is a system include
1085 : // header.
1086 : SrcMgr::CharacteristicKind FileCharacter =
1087 : std::max(HeaderInfo.getFileDirFlavor(File),
1088 568: SourceMgr.getFileCharacteristic(FilenameTok.getLocation()));
1089 :
1090 : // Look up the file, create a File ID for it.
1091 : FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
1092 568: FileCharacter);
0: branch 1 not taken
568: branch 2 taken
1093 568: if (FID.isInvalid()) {
1094 0: Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
1095 : return;
1096 : }
1097 :
1098 : // Finally, if all is good, enter the new file!
1099 568: std::string ErrorStr;
0: branch 1 not taken
568: branch 2 taken
1100 568: if (EnterSourceFile(FID, CurDir, ErrorStr))
1101 : Diag(FilenameTok, diag::err_pp_error_opening_file)
568: branch 14 taken
121: branch 15 taken
1102 0: << std::string(SourceMgr.getFileEntryForID(FID)->getName()) << ErrorStr;
1103 : }
1104 :
1105 : /// HandleIncludeNextDirective - Implements #include_next.
1106 : ///
1107 17: void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
1108 17: Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1109 :
1110 : // #include_next is like #include, except that we start searching after
1111 : // the current found directory. If we can't do this, issue a
1112 : // diagnostic.
1113 17: const DirectoryLookup *Lookup = CurDirLookup;
0: branch 1 not taken
17: branch 2 taken
1114 17: if (isInPrimaryFile()) {
1115 0: Lookup = 0;
1116 0: Diag(IncludeNextTok, diag::pp_include_next_in_primary);
0: branch 0 not taken
17: branch 1 taken
1117 17: } else if (Lookup == 0) {
1118 0: Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1119 : } else {
1120 : // Start looking up in the next directory.
1121 17: ++Lookup;
1122 : }
1123 :
1124 17: return HandleIncludeDirective(IncludeNextTok, Lookup);
1125 : }
1126 :
1127 : /// HandleImportDirective - Implements #import.
1128 : ///
1129 4: void Preprocessor::HandleImportDirective(Token &ImportTok) {
1: branch 0 taken
3: branch 1 taken
1130 4: if (!Features.ObjC1) // #import is standard for ObjC.
1131 1: Diag(ImportTok, diag::ext_pp_import_directive);
1132 :
1133 4: return HandleIncludeDirective(ImportTok, 0, true);
1134 : }
1135 :
1136 : /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
1137 : /// pseudo directive in the predefines buffer. This handles it by sucking all
1138 : /// tokens through the preprocessor and discarding them (only keeping the side
1139 : /// effects on the preprocessor).
1140 1: void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) {
1141 : // This directive should only occur in the predefines buffer. If not, emit an
1142 : // error and reject it.
1143 1: SourceLocation Loc = IncludeMacrosTok.getLocation();
0: branch 2 not taken
1: branch 3 taken
1144 1: if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) {
1145 : Diag(IncludeMacrosTok.getLocation(),
1146 0: diag::pp_include_macros_out_of_predefines);
1147 0: DiscardUntilEndOfDirective();
1148 0: return;
1149 : }
1150 :
1151 : // Treat this as a normal #include for checking purposes. If this is
1152 : // successful, it will push a new lexer onto the include stack.
1153 1: HandleIncludeDirective(IncludeMacrosTok, 0, false);
1154 :
1155 1: Token TmpTok;
0: branch 1 not taken
1: branch 2 taken
1156 1: do {
1157 1: Lex(TmpTok);
1: branch 1 taken
0: branch 2 not taken
1158 1: assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
1159 : } while (TmpTok.isNot(tok::hashhash));
1160 : }
1161 :
1162 : //===----------------------------------------------------------------------===//
1163 : // Preprocessor Macro Directive Handling.
1164 : //===----------------------------------------------------------------------===//
1165 :
1166 : /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1167 : /// definition has just been read. Lex the rest of the arguments and the
1168 : /// closing ), updating MI with what we learn. Return true if an error occurs
1169 : /// parsing the arg list.
1170 2023: bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1171 2023: llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1172 :
1173 2023: Token Tok;
1174 1038: while (1) {
1175 3061: LexUnexpandedToken(Tok);
42: branch 1 taken
33: branch 2 taken
0: branch 3 not taken
2986: branch 4 taken
1176 3061: switch (Tok.getKind()) {
1177 : case tok::r_paren:
1178 : // Found the end of the argument list.
42: branch 1 taken
0: branch 2 not taken
1179 42: if (Arguments.empty()) // #define FOO()
1180 42: return false;
1181 : // Otherwise we have #define FOO(A,)
1182 0: Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1183 0: return true;
1184 : case tok::ellipsis: // #define X(... -> C99 varargs
1185 : // Warn if use of C99 feature in non-C99 mode.
4: branch 0 taken
29: branch 1 taken
1186 33: if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1187 :
1188 : // Lex the token after the identifier.
1189 33: LexUnexpandedToken(Tok);
0: branch 1 not taken
33: branch 2 taken
1190 33: if (Tok.isNot(tok::r_paren)) {
1191 0: Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1192 0: return true;
1193 : }
1194 : // Add the __VA_ARGS__ identifier as an argument.
1195 33: Arguments.push_back(Ident__VA_ARGS__);
1196 33: MI->setIsC99Varargs();
1197 33: MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1198 33: return false;
1199 : case tok::eom: // #define X(
1200 0: Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1201 0: return true;
1202 : default:
1203 : // Handle keywords and identifiers here to accept things like
1204 : // #define Foo(for) for.
1205 2986: IdentifierInfo *II = Tok.getIdentifierInfo();
0: branch 0 not taken
2986: branch 1 taken
1206 2986: if (II == 0) {
1207 : // #define X(1
1208 0: Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1209 0: return true;
1210 : }
1211 :
1212 : // If this is already used as an argument, it is used multiple times (e.g.
1213 : // #define X(A,A.
0: branch 4 not taken
2986: branch 5 taken
1214 2986: if (std::find(Arguments.begin(), Arguments.end(), II) !=
1215 : Arguments.end()) { // C99 6.10.3p6
1216 0: Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
1217 0: return true;
1218 : }
1219 :
1220 : // Add the argument to the macro info.
1221 2986: Arguments.push_back(II);
1222 :
1223 : // Lex the token after the identifier.
1224 2986: LexUnexpandedToken(Tok);
1225 :
0: branch 1 not taken
1938: branch 2 taken
1038: branch 3 taken
10: branch 4 taken
1226 2986: switch (Tok.getKind()) {
1227 : default: // #define X(A B
1228 0: Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1229 0: return true;
1230 : case tok::r_paren: // #define X(A)
1231 1938: MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1232 1938: return false;
1233 : case tok::comma: // #define X(A,
1234 : break;
1235 : case tok::ellipsis: // #define X(A... -> GCC extension
1236 : // Diagnose extension.
1237 10: Diag(Tok, diag::ext_named_variadic_macro);
1238 :
1239 : // Lex the token after the identifier.
1240 10: LexUnexpandedToken(Tok);
0: branch 1 not taken
10: branch 2 taken
1241 10: if (Tok.isNot(tok::r_paren)) {
1242 0: Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1243 0: return true;
1244 : }
1245 :
1246 10: MI->setIsGNUVarargs();
1247 10: MI->setArgumentList(&Arguments[0], Arguments.size(), BP);
1248 10: return false;
1249 : }
1250 : }
1251 2023: }
1252 : }
1253 :
1254 : /// HandleDefineDirective - Implements #define. This consumes the entire macro
1255 : /// line then lets the caller lex the next real token.
1256 269952: void Preprocessor::HandleDefineDirective(Token &DefineTok) {
1257 269952: ++NumDefined;
1258 :
1259 269952: Token MacroNameTok;
1260 269952: ReadMacroName(MacroNameTok, 1);
1261 :
1262 : // Error reading macro name? If so, diagnostic already issued.
1: branch 1 taken
269951: branch 2 taken
1263 269952: if (MacroNameTok.is(tok::eom))
1264 1: return;
1265 :
1266 269951: Token LastTok = MacroNameTok;
1267 :
1268 : // If we are supposed to keep comments in #defines, reenable comment saving
1269 : // mode.
269951: branch 1 taken
0: branch 2 not taken
1270 269951: if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
1271 :
1272 : // Create the new macro.
1273 269951: MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation());
1274 :
1275 269951: Token Tok;
1276 269951: LexUnexpandedToken(Tok);
1277 :
1278 : // If this is a function-like macro definition, parse the argument list,
1279 : // marking each of the identifiers as being used as macro arguments. Also,
1280 : // check other constraints on the first token of the macro body.
264030: branch 1 taken
5921: branch 2 taken
1281 269951: if (Tok.is(tok::eom)) {
1282 : // If there is no body to this macro, we have no special handling here.
262005: branch 1 taken
2025: branch 2 taken
1283 264030: } else if (Tok.hasLeadingSpace()) {
1284 : // This is a normal token with leading space. Clear the leading space
1285 : // marker on the first token to get proper expansion.
1286 262005: Tok.clearFlag(Token::LeadingSpace);
2023: branch 1 taken
2: branch 2 taken
1287 2025: } else if (Tok.is(tok::l_paren)) {
1288 : // This is a function-like macro definition. Read the argument list.
1289 2023: MI->setIsFunctionLike();
0: branch 1 not taken
2023: branch 2 taken
1290 2023: if (ReadMacroDefinitionArgList(MI)) {
1291 : // Forget about MI.
1292 0: ReleaseMacroInfo(MI);
1293 : // Throw away the rest of the line.
0: branch 0 not taken
0: branch 1 not taken
1294 0: if (CurPPLexer->ParsingPreprocessorDirective)
1295 0: DiscardUntilEndOfDirective();
1296 0: return;
1297 : }
1298 :
1299 : // If this is a definition of a variadic C99 function-like macro, not using
1300 : // the GNU named varargs extension, enabled __VA_ARGS__.
1301 :
1302 : // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1303 : // This gets unpoisoned where it is allowed.
2023: branch 1 taken
0: branch 2 not taken
1304 2023: assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
33: branch 1 taken
1990: branch 2 taken
1305 2023: if (MI->isC99Varargs())
1306 33: Ident__VA_ARGS__->setIsPoisoned(false);
1307 :
1308 : // Read the first token after the arg list for down below.
1309 2023: LexUnexpandedToken(Tok);
0: branch 0 not taken
2: branch 1 taken
1310 2: } else if (Features.C99) {
1311 : // C99 requires whitespace between the macro definition and the body. Emit
1312 : // a diagnostic for something like "#define X+".
1313 0: Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1314 : } else {
1315 : // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
1316 : // first character of a replacement list is not a character required by
1317 : // subclause 5.2.1, then there shall be white-space separation between the
1318 : // identifier and the replacement list.". 5.2.1 lists this set:
1319 : // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
1320 : // is irrelevant here.
1321 2: bool isInvalid = false;
0: branch 1 not taken
2: branch 2 taken
1322 2: if (Tok.is(tok::at)) // @ is not in the list above.
1323 0: isInvalid = true;
1: branch 1 taken
1: branch 2 taken
1324 2: else if (Tok.is(tok::unknown)) {
1325 : // If we have an unknown token, it is something strange like "`". Since
1326 : // all of valid characters would have lexed into a single character
1327 : // token of some sort, we know this is not a valid case.
1328 1: isInvalid = true;
1329 : }
1: branch 0 taken
1: branch 1 taken
1330 2: if (isInvalid)
1331 1: Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
1332 : else
1333 1: Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
1334 : }
1335 :
263903: branch 1 taken
6048: branch 2 taken
1336 269951: if (!Tok.is(tok::eom))
1337 263903: LastTok = Tok;
1338 :
1339 : // Read the rest of the macro body.
267928: branch 1 taken
2023: branch 2 taken
1340 269951: if (MI->isObjectLike()) {
1341 : // Object-like macros are very simple, just read their body.
337268: branch 1 taken
267928: branch 2 taken
1342 873124: while (Tok.isNot(tok::eom)) {
1343 337268: LastTok = Tok;
1344 337268: MI->AddTokenToBody(Tok);
1345 : // Get the next token of the macro.
1346 337268: LexUnexpandedToken(Tok);
1347 : }
1348 :
1349 : } else {
1350 : // Otherwise, read the body of a function-like macro. While we are at it,
1351 : // check C99 6.10.3.2p1: ensure that # operators are followed by macro
1352 : // parameters in function-like macro expansions.
16526: branch 1 taken
2023: branch 2 taken
1353 20572: while (Tok.isNot(tok::eom)) {
1354 16526: LastTok = Tok;
1355 :
16379: branch 1 taken
147: branch 2 taken
1356 16526: if (Tok.isNot(tok::hash)) {
1357 16379: MI->AddTokenToBody(Tok);
1358 :
1359 : // Get the next token of the macro.
1360 16379: LexUnexpandedToken(Tok);
1361 16379: continue;
1362 : }
1363 :
1364 : // Get the next token of the macro.
1365 147: LexUnexpandedToken(Tok);
1366 :
1367 : // Check for a valid macro arg identifier.
145: branch 1 taken
2: branch 2 taken
2: branch 5 taken
143: branch 6 taken
4: branch 7 taken
143: branch 8 taken
1368 147: if (Tok.getIdentifierInfo() == 0 ||
1369 : MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1370 :
1371 : // If this is assembler-with-cpp mode, we accept random gibberish after
1372 : // the '#' because '#' is often a comment character. However, change
1373 : // the kind of the token to tok::unknown so that the preprocessor isn't
1374 : // confused.
4: branch 1 taken
0: branch 2 not taken
4: branch 4 taken
0: branch 5 not taken
4: branch 6 taken
0: branch 7 not taken
1375 4: if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) {
1376 4: LastTok.setKind(tok::unknown);
1377 : } else {
1378 0: Diag(Tok, diag::err_pp_stringize_not_parameter);
1379 0: ReleaseMacroInfo(MI);
1380 :
1381 : // Disable __VA_ARGS__ again.
1382 0: Ident__VA_ARGS__->setIsPoisoned(true);
1383 0: return;
1384 : }
1385 : }
1386 :
1387 : // Things look ok, add the '#' and param name tokens to the macro.
1388 147: MI->AddTokenToBody(LastTok);
1389 147: MI->AddTokenToBody(Tok);
1390 147: LastTok = Tok;
1391 :
1392 : // Get the next token of the macro.
1393 147: LexUnexpandedToken(Tok);
1394 : }
1395 : }
1396 :
1397 :
1398 : // Disable __VA_ARGS__ again.
1399 269951: Ident__VA_ARGS__->setIsPoisoned(true);
1400 :
1401 : // Check that there is no paste (##) operator at the begining or end of the
1402 : // replacement list.
1403 269951: unsigned NumTokens = MI->getNumTokens();
263903: branch 0 taken
6048: branch 1 taken
1404 269951: if (NumTokens != 0) {
6: branch 2 taken
263897: branch 3 taken
1405 263903: if (MI->getReplacementToken(0).is(tok::hashhash)) {
1406 6: Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
1407 6: ReleaseMacroInfo(MI);
1408 6: return;
1409 : }
4: branch 2 taken
263893: branch 3 taken
1410 263897: if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
1411 4: Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1412 4: ReleaseMacroInfo(MI);
1413 4: return;
1414 : }
1415 : }
1416 :
1417 : // If this is the primary source file, remember that this macro hasn't been
1418 : // used yet.
623: branch 1 taken
269318: branch 2 taken
1419 269941: if (isInPrimaryFile())
1420 623: MI->setIsUsed(false);
1421 :
1422 269941: MI->setDefinitionEndLoc(LastTok.getLocation());
1423 :
1424 : // Finally, if this identifier already had a macro defined for it, verify that
1425 : // the macro bodies are identical and free the old definition.
587: branch 2 taken
269354: branch 3 taken
1426 269941: if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) {
1427 : // It is very common for system headers to have tons of macro redefinitions
1428 : // and for warnings to be disabled in system headers. If this is the case,
1429 : // then don't bother calling MacroInfo::isIdenticalTo.
587: branch 2 taken
0: branch 3 not taken
9: branch 6 taken
578: branch 7 taken
9: branch 8 taken
578: branch 9 taken
1430 587: if (!getDiagnostics().getSuppressSystemWarnings() ||
1431 : !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
3: branch 1 taken
6: branch 2 taken
1432 9: if (!OtherMI->isUsed())
1433 3: Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1434 :
1435 : // Macros must be identical. This means all tokes and whitespace
1436 : // separation must be the same. C99 6.10.3.2.
4: branch 1 taken
5: branch 2 taken
1437 9: if (!MI->isIdenticalTo(*OtherMI, *this)) {
1438 : Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
1439 4: << MacroNameTok.getIdentifierInfo();
1440 4: Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
1441 : }
1442 : }
1443 :
1444 587: ReleaseMacroInfo(OtherMI);
1445 : }
1446 :
1447 269941: setMacroInfo(MacroNameTok.getIdentifierInfo(), MI);
1448 :
1449 : // If the callbacks want to know, tell them about the macro definition.
23521: branch 0 taken
246420: branch 1 taken
1450 269941: if (Callbacks)
1451 23521: Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI);
1452 : }
1453 :
1454 : /// HandleUndefDirective - Implements #undef.
1455 : ///
1456 1039: void Preprocessor::HandleUndefDirective(Token &UndefTok) {
1457 1039: ++NumUndefined;
1458 :
1459 1039: Token MacroNameTok;
1460 1039: ReadMacroName(MacroNameTok, 2);
1461 :
1462 : // Error reading macro name? If so, diagnostic already issued.
0: branch 1 not taken
1039: branch 2 taken
1463 1039: if (MacroNameTok.is(tok::eom))
1464 0: return;
1465 :
1466 : // Check to see if this is the last token on the #undef line.
1467 1039: CheckEndOfDirective("undef");
1468 :
1469 : // Okay, we finally have a valid identifier to undef.
1470 1039: MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo());
1471 :
1472 : // If the macro is not defined, this is a noop undef, just return.
156: branch 0 taken
883: branch 1 taken
1473 1039: if (MI == 0) return;
1474 :
2: branch 1 taken
154: branch 2 taken
1475 156: if (!MI->isUsed())
1476 2: Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
1477 :
1478 : // If the callbacks want to know, tell them about the macro #undef.
71: branch 0 taken
85: branch 1 taken
1479 156: if (Callbacks)
1480 71: Callbacks->MacroUndefined(MacroNameTok.getIdentifierInfo(), MI);
1481 :
1482 : // Free macro definition.
1483 156: ReleaseMacroInfo(MI);
1484 156: setMacroInfo(MacroNameTok.getIdentifierInfo(), 0);
1485 : }
1486 :
1487 :
1488 : //===----------------------------------------------------------------------===//
1489 : // Preprocessor Conditional Directive Handling.
1490 : //===----------------------------------------------------------------------===//
1491 :
1492 : /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1493 : /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1494 : /// if any tokens have been returned or pp-directives activated before this
1495 : /// #ifndef has been lexed.
1496 : ///
1497 : void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
1498 2596: bool ReadAnyTokensBeforeDirective) {
1499 2596: ++NumIf;
1500 2596: Token DirectiveTok = Result;
1501 :
1502 2596: Token MacroNameTok;
1503 2596: ReadMacroName(MacroNameTok);
1504 :
1505 : // Error reading macro name? If so, diagnostic already issued.
1: branch 1 taken
2595: branch 2 taken
1506 2596: if (MacroNameTok.is(tok::eom)) {
1507 : // Skip code until we get to #endif. This helps with recovery by not
1508 : // emitting an error when the #endif is reached.
1509 : SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1510 1: /*Foundnonskip*/false, /*FoundElse*/false);
1511 1: return;
1512 : }
1513 :
1514 : // Check to see if this is the last token on the #if[n]def line.
1054: branch 0 taken
1541: branch 1 taken
1515 2595: CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
1516 :
546: branch 1 taken
2049: branch 2 taken
1517 2595: if (CurPPLexer->getConditionalStackDepth() == 0) {
1518 : // If the start of a top-level #ifdef, inform MIOpt.
352: branch 0 taken
194: branch 1 taken
1519 546: if (!ReadAnyTokensBeforeDirective) {
0: branch 0 not taken
352: branch 1 taken
1520 352: assert(isIfndef && "#ifdef shouldn't reach here");
1521 352: CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1522 : } else
1523 194: CurPPLexer->MIOpt.EnterTopLevelConditional();
1524 : }
1525 :
1526 2595: IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1527 2595: MacroInfo *MI = getMacroInfo(MII);
1528 :
1529 : // If there is a macro, process it.
960: branch 0 taken
1635: branch 1 taken
1530 2595: if (MI) // Mark it used.
1531 960: MI->setIsUsed(true);
1532 :
1533 : // Should we include the stuff contained by this directive?
1740: branch 0 taken
855: branch 1 taken
1534 2595: if (!MI == isIfndef) {
1535 : // Yes, remember that we are inside a conditional, then lex the next token.
1536 : CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
1537 : /*wasskip*/false, /*foundnonskip*/true,
1538 1740: /*foundelse*/false);
1539 : } else {
1540 : // No, skip the contents of this block and return the first token after it.
1541 : SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1542 : /*Foundnonskip*/false,
1543 855: /*FoundElse*/false);
1544 : }
1545 : }
1546 :
1547 : /// HandleIfDirective - Implements the #if directive.
1548 : ///
1549 : void Preprocessor::HandleIfDirective(Token &IfToken,
1550 2307: bool ReadAnyTokensBeforeDirective) {
1551 2307: ++NumIf;
1552 :
1553 : // Parse and evaluation the conditional expression.
1554 2307: IdentifierInfo *IfNDefMacro = 0;
1555 2307: bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
1556 :
1557 :
1558 : // If this condition is equivalent to #ifndef X, and if this is the first
1559 : // directive seen, handle it for the multiple-include optimization.
351: branch 1 taken
1956: branch 2 taken
1560 2307: if (CurPPLexer->getConditionalStackDepth() == 0) {
177: branch 0 taken
174: branch 1 taken
4: branch 2 taken
173: branch 3 taken
1561 355: if (!ReadAnyTokensBeforeDirective && IfNDefMacro)
1562 4: CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1563 : else
1564 347: CurPPLexer->MIOpt.EnterTopLevelConditional();
1565 : }
1566 :
1567 : // Should we include the stuff contained by this directive?
1160: branch 0 taken
1147: branch 1 taken
1568 2307: if (ConditionalTrue) {
1569 : // Yes, remember that we are inside a conditional, then lex the next token.
1570 : CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
1571 1160: /*foundnonskip*/true, /*foundelse*/false);
1572 : } else {
1573 : // No, skip the contents of this block and return the first token after it.
1574 : SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
1575 1147: /*FoundElse*/false);
1576 : }
1577 2307: }
1578 :
1579 : /// HandleEndifDirective - Implements the #endif directive.
1580 : ///
1581 2973: void Preprocessor::HandleEndifDirective(Token &EndifToken) {
1582 2973: ++NumEndif;
1583 :
1584 : // Check that this is the whole directive.
1585 2973: CheckEndOfDirective("endif");
1586 :
1587 2973: PPConditionalInfo CondInfo;
0: branch 1 not taken
2973: branch 2 taken
1588 2973: if (CurPPLexer->popConditionalLevel(CondInfo)) {
1589 : // No conditionals on the stack: this is an #endif without an #if.
1590 0: Diag(EndifToken, diag::err_pp_endif_without_if);
1591 0: return;
1592 : }
1593 :
1594 : // If this the end of a top-level #endif, inform MIOpt.
590: branch 1 taken
2383: branch 2 taken
1595 2973: if (CurPPLexer->getConditionalStackDepth() == 0)
1596 590: CurPPLexer->MIOpt.ExitTopLevelConditional();
1597 :
1598 : assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
2973: branch 0 taken
0: branch 1 not taken
0: branch 2 not taken
2973: branch 3 taken
1599 2973: "This code should only be reachable in the non-skipping case!");
1600 : }
1601 :
1602 :
1603 609: void Preprocessor::HandleElseDirective(Token &Result) {
1604 609: ++NumElse;
1605 :
1606 : // #else directive in a non-skipping conditional... start skipping.
1607 609: CheckEndOfDirective("else");
1608 :
1609 609: PPConditionalInfo CI;
0: branch 1 not taken
609: branch 2 taken
1610 609: if (CurPPLexer->popConditionalLevel(CI)) {
1611 0: Diag(Result, diag::pp_err_else_without_if);
1612 0: return;
1613 : }
1614 :
1615 : // If this is a top-level #else, inform the MIOpt.
46: branch 1 taken
563: branch 2 taken
1616 609: if (CurPPLexer->getConditionalStackDepth() == 0)
1617 46: CurPPLexer->MIOpt.EnterTopLevelConditional();
1618 :
1619 : // If this is a #else with a #else before it, report the error.
0: branch 0 not taken
609: branch 1 taken
1620 609: if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
1621 :
1622 : // Finally, skip the rest of the contents of this block and return the first
1623 : // token after it.
1624 : return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1625 609: /*FoundElse*/true);
1626 : }
1627 :
1628 43: void Preprocessor::HandleElifDirective(Token &ElifToken) {
1629 43: ++NumElse;
1630 :
1631 : // #elif directive in a non-skipping conditional... start skipping.
1632 : // We don't care what the condition is, because we will always skip it (since
1633 : // the block immediately before it was included).
1634 43: DiscardUntilEndOfDirective();
1635 :
1636 43: PPConditionalInfo CI;
0: branch 1 not taken
43: branch 2 taken
1637 43: if (CurPPLexer->popConditionalLevel(CI)) {
1638 0: Diag(ElifToken, diag::pp_err_elif_without_if);
1639 0: return;
1640 : }
1641 :
1642 : // If this is a top-level #elif, inform the MIOpt.
28: branch 1 taken
15: branch 2 taken
1643 43: if (CurPPLexer->getConditionalStackDepth() == 0)
1644 28: CurPPLexer->MIOpt.EnterTopLevelConditional();
1645 :
1646 : // If this is a #elif with a #else before it, report the error.
0: branch 0 not taken
43: branch 1 taken
1647 43: if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
1648 :
1649 : // Finally, skip the rest of the contents of this block and return the first
1650 : // token after it.
1651 : return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1652 43: /*FoundElse*/CI.FoundElse);
1653 : }
1654 :
Generated: 2010-02-10 01:31 by zcov