blob: 884fdf77946a7da63006db8d0f96f9d6bfd8d400 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner80965422006-07-04 18:03:19 +000010// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14//
Chris Lattner6df79752007-04-04 06:54:19 +000015// FIXME: implement testing for #assert's.
Chris Lattner22eb9722006-06-18 05:43:12 +000016// FIXME: Track signed/unsigned correctly.
17// FIXME: Track and report integer overflow correctly.
18//
19//===----------------------------------------------------------------------===//
20
21#include "clang/Lex/Preprocessor.h"
Chris Lattnera78a97e2006-07-03 05:42:18 +000022#include "clang/Lex/MacroInfo.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000023#include "clang/Lex/LiteralSupport.h"
Chris Lattner81278c62006-10-14 19:03:49 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000025#include "clang/Basic/TokenKinds.h"
26#include "clang/Basic/Diagnostic.h"
Chris Lattnerce5dc8a2007-04-04 06:46:55 +000027#include "llvm/ADT/APInt.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000028#include "llvm/ADT/SmallString.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000029using namespace llvm;
30using namespace clang;
31
Chris Lattnerce5dc8a2007-04-04 06:46:55 +000032static bool EvaluateDirectiveSubExpr(APInt &LHS, unsigned MinPrec,
Chris Lattnere3519cc2006-07-04 18:11:39 +000033 LexerToken &PeekTok, Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000034
Chris Lattnerb9d90f72006-07-04 18:32:03 +000035/// DefinedTracker - This struct is used while parsing expressions to keep track
36/// of whether !defined(X) has been seen.
37///
38/// With this simple scheme, we handle the basic forms:
39/// !defined(X) and !defined X
40/// but we also trivially handle (silly) stuff like:
41/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
42struct DefinedTracker {
43 /// Each time a Value is evaluated, it returns information about whether the
44 /// parsed value is of the form defined(X), !defined(X) or is something else.
45 enum TrackerState {
46 DefinedMacro, // defined(X)
47 NotDefinedMacro, // !defined(X)
48 Unknown // Something else.
49 } State;
50 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
51 /// indicates the macro that was checked.
52 IdentifierInfo *TheMacro;
53};
54
55
Chris Lattner22eb9722006-06-18 05:43:12 +000056
57/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
58/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +000059/// parsing. This function also returns information about the form of the
60/// expression in DT. See above for information on what DT means.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +000061static bool EvaluateValue(APInt &Result, LexerToken &PeekTok,
62 DefinedTracker &DT, Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +000063 Result = 0;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000064 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +000065
66 // If this token's spelling is a pp-identifier, check to see if it is
67 // 'defined' or if it is a macro. Note that we check here because many
68 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +000069 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner22eb9722006-06-18 05:43:12 +000070 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
71 // into a simple 0.
Chris Lattner2bb8a952006-11-21 22:24:17 +000072 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattner22eb9722006-06-18 05:43:12 +000073 Result = 0;
Chris Lattnerbcb416b2006-10-27 05:43:50 +000074 PP.LexNonComment(PeekTok);
Chris Lattnercb283342006-06-18 06:48:37 +000075 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +000076 }
77
78 // Handle "defined X" and "defined(X)".
Chris Lattner22eb9722006-06-18 05:43:12 +000079
Chris Lattnere3519cc2006-07-04 18:11:39 +000080 // Get the next token, don't expand it.
81 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000082
83 // Two options, it can either be a pp-identifier or a (.
84 bool InParens = false;
85 if (PeekTok.getKind() == tok::l_paren) {
86 // Found a paren, remember we saw it and skip it.
87 InParens = true;
Chris Lattnere3519cc2006-07-04 18:11:39 +000088 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000089 }
90
91 // If we don't have a pp-identifier now, this is an error.
92 if ((II = PeekTok.getIdentifierInfo()) == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +000093 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +000094 return true;
95 }
96
97 // Otherwise, we got an identifier, is it defined to something?
98 Result = II->getMacroInfo() != 0;
Chris Lattnera78a97e2006-07-03 05:42:18 +000099
100 // If there is a macro, mark it used.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000101 if (Result != 0) {
Chris Lattner81278c62006-10-14 19:03:49 +0000102 II->getMacroInfo()->setIsUsed(true);
103
104 // If this is the first use of a target-specific macro, warn about it.
105 if (II->getMacroInfo()->isTargetSpecific()) {
106 // Don't warn on second use.
107 II->getMacroInfo()->setIsTargetSpecific(false);
108 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
109 diag::port_target_macro_use);
110 }
Chris Lattner063400e2006-10-14 19:54:15 +0000111 } else {
112 // Use of a target-specific macro for some other target? If so, warn.
113 if (II->isOtherTargetMacro()) {
114 II->setIsOtherTargetMacro(false); // Don't warn on second use.
115 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
116 diag::port_target_macro_use);
117 }
Chris Lattner81278c62006-10-14 19:03:49 +0000118 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000119
120 // Consume identifier.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000121 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000122
123 // If we are in parens, ensure we have a trailing ).
124 if (InParens) {
125 if (PeekTok.getKind() != tok::r_paren) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000126 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000127 return true;
128 }
129 // Consume the ).
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000130 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000131 }
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000132
133 // Success, remember that we saw defined(X).
134 DT.State = DefinedTracker::DefinedMacro;
135 DT.TheMacro = II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000136 return false;
137 }
138
139 switch (PeekTok.getKind()) {
140 default: // Non-value token.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000141 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000142 return true;
143 case tok::eom:
144 case tok::r_paren:
145 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000146 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000147 return true;
148 case tok::numeric_constant: {
Chris Lattner6df79752007-04-04 06:54:19 +0000149 SmallString<64> IntegerBuffer;
Steve Naroff451d8f162007-03-12 23:22:38 +0000150 IntegerBuffer.resize(PeekTok.getLength());
151 const char *ThisTokBegin = &IntegerBuffer[0];
152 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
153 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
154 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000155 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000156 return true; // a diagnostic was already reported.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000157
158 if (Literal.isFloatingLiteral()) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000159 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000160 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000161 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000162 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattner6df79752007-04-04 06:54:19 +0000163
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000164 // FIXME: Handle overflow based on whether the value is signed. If signed
165 // and if the value is too large, emit a warning "integer constant is so
166 // large that it is unsigned" e.g. 12345678901234567890.
167 if (Literal.GetIntegerValue(Result))
168 PP.Diag(PeekTok, diag::warn_integer_too_large);
169 PP.LexNonComment(PeekTok);
170 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000171 }
172 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000173 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000174 // Parse the value and if there are any binary operators involved, parse
175 // them.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000176 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000177
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000178 // If this is a silly value like (X), which doesn't need parens, check for
179 // !(defined X).
180 if (PeekTok.getKind() == tok::r_paren) {
181 // Just use DT unmodified as our result.
182 } else {
183 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, PP)) return true;
184
185 if (PeekTok.getKind() != tok::r_paren) {
186 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
187 return true;
188 }
189 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000190 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000191 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000192 return false;
193
194 case tok::plus:
195 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000196 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000197 return EvaluateValue(Result, PeekTok, DT, PP);
Chris Lattner22eb9722006-06-18 05:43:12 +0000198 case tok::minus:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000199 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000200 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000201 Result = -Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000202 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000203 return false;
204
205 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000206 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000207 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000208 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000209 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000210 return false;
211
212 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000213 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000214 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000215 Result = !Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000216
217 if (DT.State == DefinedTracker::DefinedMacro)
218 DT.State = DefinedTracker::NotDefinedMacro;
219 else if (DT.State == DefinedTracker::NotDefinedMacro)
220 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000221 return false;
222
223 // FIXME: Handle #assert
224 }
225}
226
227
228
229/// getPrecedence - Return the precedence of the specified binary operator
230/// token. This returns:
231/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000232/// 14 - *,/,%
233/// 13 - -,+
234/// 12 - <<,>>
235/// 11 - >=, <=, >, <
236/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000237/// 9 - &
238/// 8 - ^
239/// 7 - |
240/// 6 - &&
241/// 5 - ||
242/// 4 - ?
243/// 3 - :
244/// 0 - eom, )
245static unsigned getPrecedence(tok::TokenKind Kind) {
246 switch (Kind) {
247 default: return ~0U;
248 case tok::percent:
249 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000250 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000251 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000252 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000253 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000254 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000255 case tok::lessequal:
256 case tok::less:
257 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000258 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000259 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000260 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000261 case tok::amp: return 9;
262 case tok::caret: return 8;
263 case tok::pipe: return 7;
264 case tok::ampamp: return 6;
265 case tok::pipepipe: return 5;
266 case tok::question: return 4;
267 case tok::colon: return 3;
268 case tok::comma: return 2;
269 case tok::r_paren: return 0; // Lowest priority, end of expr.
270 case tok::eom: return 0; // Lowest priority, end of macro.
271 }
272}
273
274
275/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
276/// PeekTok, and whose precedence is PeekPrec.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000277static bool EvaluateDirectiveSubExpr(APInt &LHS, unsigned MinPrec,
Chris Lattnere3519cc2006-07-04 18:11:39 +0000278 LexerToken &PeekTok, Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000279 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
280 // If this token isn't valid, report the error.
281 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000282 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000283 return true;
284 }
285
286 while (1) {
287 // If this token has a lower precedence than we are allowed to parse, return
288 // it so that higher levels of the recursion can parse it.
289 if (PeekPrec < MinPrec)
290 return false;
291
292 tok::TokenKind Operator = PeekTok.getKind();
293
294 // Consume the operator, saving the operator token for error reporting.
295 LexerToken OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000296 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000297
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000298 APInt RHS(LHS.getBitWidth(), 0);
Chris Lattner22eb9722006-06-18 05:43:12 +0000299 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000300 DefinedTracker DT;
301 if (EvaluateValue(RHS, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000302
303 // Remember the precedence of this operator and get the precedence of the
304 // operator immediately to the right of the RHS.
305 unsigned ThisPrec = PeekPrec;
306 PeekPrec = getPrecedence(PeekTok.getKind());
307
308 // If this token isn't valid, report the error.
309 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000310 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000311 return true;
312 }
313
314 bool isRightAssoc = Operator == tok::question;
315
316 // Get the precedence of the operator to the right of the RHS. If it binds
317 // more tightly with RHS than we do, evaluate it completely first.
318 if (ThisPrec < PeekPrec ||
319 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000320 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000321 return true;
322 PeekPrec = getPrecedence(PeekTok.getKind());
323 }
324 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
325
326 switch (Operator) {
327 default: assert(0 && "Unknown operator token!");
328 case tok::percent:
329 if (RHS == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000330 PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000331 return true;
332 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000333 // FIXME: sign.
334 LHS = LHS.urem(RHS);
Chris Lattner22eb9722006-06-18 05:43:12 +0000335 break;
336 case tok::slash:
337 if (RHS == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000338 PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000339 return true;
340 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000341 // FIXME: sign.
342 LHS = LHS.udiv(RHS);
Chris Lattner22eb9722006-06-18 05:43:12 +0000343 break;
344 case tok::star : LHS *= RHS; break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000345 case tok::lessless:
346 // FIXME: shift amt overflow?
347 // FIXME: Don't use getZExtValue.
348 LHS = LHS << RHS.getZExtValue();
349 break;
350 case tok::greatergreater:
351 // FIXME: signed vs unsigned
352 // FIXME: Don't use getZExtValue.
353 LHS = LHS.ashr(RHS.getZExtValue());
354 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000355 case tok::plus : LHS += RHS; break;
356 case tok::minus: LHS -= RHS; break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000357 case tok::lessequal:
358 // FIXME: signed vs unsigned
359 LHS = LHS.sle(RHS);
360 break;
361 case tok::less:
362 // FIXME: signed vs unsigned
363 LHS = LHS.slt(RHS);
364 break;
365 case tok::greaterequal:
366 // FIXME: signed vs unsigned
367 LHS = LHS.sge(RHS);
368 break;
369 case tok::greater:
370 // FIXME: signed vs unsigned
371 LHS = LHS.sgt(RHS);
372 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000373 case tok::exclaimequal: LHS = LHS != RHS; break;
374 case tok::equalequal: LHS = LHS == RHS; break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000375 case tok::amp: LHS &= RHS; break;
376 case tok::caret: LHS ^= RHS; break;
377 case tok::pipe: LHS |= RHS; break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000378 case tok::ampamp: LHS = LHS != 0 && RHS != 0; break;
379 case tok::pipepipe: LHS = LHS != 0 || RHS != 0; break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000380 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000381 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000382 LHS = RHS; // LHS = LHS,RHS -> RHS.
383 break;
384 case tok::question: {
385 // Parse the : part of the expression.
386 if (PeekTok.getKind() != tok::colon) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000387 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000388 return true;
389 }
390 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000391 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000392
393 // Evaluate the value after the :.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000394 APInt AfterColonVal(LHS.getBitWidth(), 0);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000395 DefinedTracker DT;
396 if (EvaluateValue(AfterColonVal, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000397
398 // Parse anything after the : RHS that has a higher precedence than ?.
399 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattnere3519cc2006-07-04 18:11:39 +0000400 PeekTok, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000401 return true;
402
403 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000404 LHS = LHS != 0 ? RHS : AfterColonVal;
Chris Lattner22eb9722006-06-18 05:43:12 +0000405
406 // Figure out the precedence of the token after the : part.
407 PeekPrec = getPrecedence(PeekTok.getKind());
408 break;
409 }
410 case tok::colon:
411 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000412 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000413 return true;
414 }
415 }
416
417 return false;
418}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000419
420/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
421/// may occur after a #if or #elif directive. If the expression is equivalent
422/// to "!defined(X)" return X in IfNDefMacro.
423bool Preprocessor::
424EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
425 // Peek ahead one token.
426 LexerToken Tok;
427 Lex(Tok);
428
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000429 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
430 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
431 APInt ResVal(BitWidth, 0);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000432 DefinedTracker DT;
433 if (EvaluateValue(ResVal, Tok, DT, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000434 // Parse error, skip the rest of the macro line.
435 if (Tok.getKind() != tok::eom)
436 DiscardUntilEndOfDirective();
437 return false;
438 }
439
440 // If we are at the end of the expression after just parsing a value, there
441 // must be no (unparenthesized) binary operators involved, so we can exit
442 // directly.
443 if (Tok.getKind() == tok::eom) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000444 // If the expression we parsed was of the form !defined(macro), return the
445 // macro in IfNDefMacro.
446 if (DT.State == DefinedTracker::NotDefinedMacro)
447 IfNDefMacro = DT.TheMacro;
448
Chris Lattnere3519cc2006-07-04 18:11:39 +0000449 return ResVal != 0;
450 }
451
452 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
453 // operator and the stuff after it.
454 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, *this)) {
455 // Parse error, skip the rest of the macro line.
456 if (Tok.getKind() != tok::eom)
457 DiscardUntilEndOfDirective();
458 return false;
459 }
460
461 // If we aren't at the tok::eom token, something bad happened, like an extra
462 // ')' token.
463 if (Tok.getKind() != tok::eom) {
464 Diag(Tok, diag::err_pp_expected_eol);
465 DiscardUntilEndOfDirective();
466 }
467
468 return ResVal != 0;
469}
470