blob: 20f624a0bb12a321b6c8259ce151f3f13931ec45 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
12//
13//===----------------------------------------------------------------------===//
14//
15// FIXME: implement testing for #assert's.
16//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/MacroInfo.h"
21#include "clang/Lex/LiteralSupport.h"
Douglas Gregor1fbb4472010-08-24 20:21:13 +000022#include "clang/Lex/CodeCompletionHandler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000024#include "clang/Lex/LexDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/APSInt.h"
David Blaikie9fe8c742011-09-23 05:35:21 +000026#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
Dan Gohman3c46e8d2010-07-26 21:25:24 +000029namespace {
30
Chris Lattner8ed30442008-05-05 06:45:50 +000031/// PPValue - Represents the value of a subexpression of a preprocessor
32/// conditional and the source range covered by it.
33class PPValue {
34 SourceRange Range;
35public:
36 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +000037
Chris Lattner8ed30442008-05-05 06:45:50 +000038 // Default ctor - Construct an 'invalid' PPValue.
39 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump1eb44332009-09-09 15:08:12 +000040
Chris Lattner8ed30442008-05-05 06:45:50 +000041 unsigned getBitWidth() const { return Val.getBitWidth(); }
42 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Chris Lattner8ed30442008-05-05 06:45:50 +000044 const SourceRange &getRange() const { return Range; }
Mike Stump1eb44332009-09-09 15:08:12 +000045
Chris Lattner8ed30442008-05-05 06:45:50 +000046 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
47 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump1eb44332009-09-09 15:08:12 +000048 Range.setBegin(B); Range.setEnd(E);
Chris Lattner8ed30442008-05-05 06:45:50 +000049 }
50 void setBegin(SourceLocation L) { Range.setBegin(L); }
51 void setEnd(SourceLocation L) { Range.setEnd(L); }
52};
53
Dan Gohman3c46e8d2010-07-26 21:25:24 +000054}
55
Chris Lattner8ed30442008-05-05 06:45:50 +000056static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000057 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000058 Preprocessor &PP);
59
60/// DefinedTracker - This struct is used while parsing expressions to keep track
61/// of whether !defined(X) has been seen.
62///
63/// With this simple scheme, we handle the basic forms:
64/// !defined(X) and !defined X
65/// but we also trivially handle (silly) stuff like:
66/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
67struct DefinedTracker {
68 /// Each time a Value is evaluated, it returns information about whether the
69 /// parsed value is of the form defined(X), !defined(X) or is something else.
70 enum TrackerState {
71 DefinedMacro, // defined(X)
72 NotDefinedMacro, // !defined(X)
73 Unknown // Something else.
74 } State;
75 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
76 /// indicates the macro that was checked.
77 IdentifierInfo *TheMacro;
78};
79
John Thompsona28cc092009-10-30 13:49:06 +000080/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattnera3e008a2009-12-14 05:00:18 +000081static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
82 bool ValueLive, Preprocessor &PP) {
John Thompsona28cc092009-10-30 13:49:06 +000083 IdentifierInfo *II;
84 Result.setBegin(PeekTok.getLocation());
85
86 // Get the next token, don't expand it.
Eli Friedman88710f22011-08-03 00:04:13 +000087 PP.LexUnexpandedNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +000088
89 // Two options, it can either be a pp-identifier or a (.
90 SourceLocation LParenLoc;
91 if (PeekTok.is(tok::l_paren)) {
92 // Found a paren, remember we saw it and skip it.
93 LParenLoc = PeekTok.getLocation();
Eli Friedman88710f22011-08-03 00:04:13 +000094 PP.LexUnexpandedNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +000095 }
96
Douglas Gregor1fbb4472010-08-24 20:21:13 +000097 if (PeekTok.is(tok::code_completion)) {
98 if (PP.getCodeCompletionHandler())
99 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000100 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000101 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000102 }
103
John Thompsona28cc092009-10-30 13:49:06 +0000104 // If we don't have a pp-identifier now, this is an error.
105 if ((II = PeekTok.getIdentifierInfo()) == 0) {
106 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
107 return true;
108 }
109
110 // Otherwise, we got an identifier, is it defined to something?
111 Result.Val = II->hasMacroDefinition();
112 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
113
114 // If there is a macro, mark it used.
115 if (Result.Val != 0 && ValueLive) {
116 MacroInfo *Macro = PP.getMacroInfo(II);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000117 PP.markMacroAsUsed(Macro);
John Thompsona28cc092009-10-30 13:49:06 +0000118 }
119
Douglas Gregor3d5f9552011-10-14 00:49:43 +0000120 // Invoke the 'defined' callback.
121 if (PPCallbacks *Callbacks = PP.getPPCallbacks())
122 Callbacks->Defined(PeekTok);
123
John Thompsona28cc092009-10-30 13:49:06 +0000124 // If we are in parens, ensure we have a trailing ).
125 if (LParenLoc.isValid()) {
Eli Friedman88710f22011-08-03 00:04:13 +0000126 // Consume identifier.
127 Result.setEnd(PeekTok.getLocation());
128 PP.LexUnexpandedNonComment(PeekTok);
129
John Thompsona28cc092009-10-30 13:49:06 +0000130 if (PeekTok.isNot(tok::r_paren)) {
131 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
132 PP.Diag(LParenLoc, diag::note_matching) << "(";
133 return true;
134 }
135 // Consume the ).
136 Result.setEnd(PeekTok.getLocation());
137 PP.LexNonComment(PeekTok);
Eli Friedman88710f22011-08-03 00:04:13 +0000138 } else {
139 // Consume identifier.
140 Result.setEnd(PeekTok.getLocation());
141 PP.LexNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000142 }
143
144 // Success, remember that we saw defined(X).
145 DT.State = DefinedTracker::DefinedMacro;
146 DT.TheMacro = II;
147 return false;
148}
149
Reid Spencer5f016e22007-07-11 17:01:13 +0000150/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
151/// return the computed value in Result. Return true if there was an error
152/// parsing. This function also returns information about the form of the
153/// expression in DT. See above for information on what DT means.
154///
155/// If ValueLive is false, then this value is being evaluated in a context where
156/// the result is not used. As such, avoid diagnostics that relate to
157/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000158static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
159 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregorf29c5232010-08-24 22:20:20 +0000162 if (PeekTok.is(tok::code_completion)) {
163 if (PP.getCodeCompletionHandler())
164 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000165 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000166 PP.LexNonComment(PeekTok);
Douglas Gregorf29c5232010-08-24 22:20:20 +0000167 }
168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 // If this token's spelling is a pp-identifier, check to see if it is
170 // 'defined' or if it is a macro. Note that we check here because many
171 // keywords are pp-identifiers, so we can't check the kind.
172 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000173 // Handle "defined X" and "defined(X)".
174 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000175 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000176
177 // If this identifier isn't 'defined' or one of the special
178 // preprocessor keywords and it wasn't macro expanded, it turns
179 // into a simple 0, unless it is the C++ keyword "true", in which case it
180 // turns into "1".
181 if (ValueLive)
182 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
183 Result.Val = II->getTokenID() == tok::kw_true;
184 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
185 Result.setRange(PeekTok.getLocation());
186 PP.LexNonComment(PeekTok);
187 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 switch (PeekTok.getKind()) {
191 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000192 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000194 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 case tok::r_paren:
196 // If there is no expression, report and exit.
197 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
198 return true;
199 case tok::numeric_constant: {
200 llvm::SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000201 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000202 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000203 &NumberInvalid);
204 if (NumberInvalid)
205 return true; // a diagnostic was already reported
206
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000207 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 PeekTok.getLocation(), PP);
209 if (Literal.hadError)
210 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner6e400c22007-08-26 03:29:23 +0000212 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
214 return true;
215 }
216 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
217
Neil Boothb9449512007-08-29 22:00:19 +0000218 // long long is a C99 feature.
Richard Smith661a9962011-10-15 01:18:56 +0000219 if (!PP.getLangOptions().C99 && Literal.isLongLong)
220 PP.Diag(PeekTok, PP.getLangOptions().CPlusPlus0x ?
221 diag::warn_cxx98_compat_longlong : diag::ext_longlong);
Neil Boothb9449512007-08-29 22:00:19 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000224 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 // Overflow parsing integer literal.
226 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000227 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 } else {
229 // Set the signedness of the result to match whether there was a U suffix
230 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000231 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // Detect overflow based on whether the value is signed. If signed
234 // and if the value is too large, emit a warning "integer constant is so
235 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
236 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000237 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000238 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
239 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000240 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
241 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 }
243 }
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000246 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 PP.LexNonComment(PeekTok);
248 return false;
249 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000250 case tok::char_constant: // 'x'
251 case tok::wide_char_constant: { // L'x'
252 case tok::utf16_char_constant: // u'x'
253 case tok::utf32_char_constant: // U'x'
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 llvm::SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000255 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000256 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000257 if (CharInvalid)
258 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000259
260 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000261 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 if (Literal.hadError())
263 return true; // A diagnostic was already emitted.
264
265 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000266 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000267 unsigned NumBits;
268 if (Literal.isMultiChar())
269 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000270 else if (Literal.isWide())
271 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000272 else if (Literal.isUTF16())
273 NumBits = TI.getChar16Width();
274 else if (Literal.isUTF32())
275 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000276 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000277 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // Set the width.
280 llvm::APSInt Val(NumBits);
281 // Set the value.
282 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000283 // Set the signedness. UTF-16 and UTF-32 are always unsigned
284 if (!Literal.isUTF16() && !Literal.isUTF32())
285 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattner8ed30442008-05-05 06:45:50 +0000287 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
288 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000290 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000292 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 }
294
295 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000296 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 PP.LexNonComment(PeekTok);
298 return false;
299 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000300 case tok::l_paren: {
301 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 PP.LexNonComment(PeekTok); // Eat the (.
303 // Parse the value and if there are any binary operators involved, parse
304 // them.
305 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
306
307 // If this is a silly value like (X), which doesn't need parens, check for
308 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000309 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Just use DT unmodified as our result.
311 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000312 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
314 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000316 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000317 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
318 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000319 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 return true;
321 }
322 DT.State = DefinedTracker::Unknown;
323 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000324 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 PP.LexNonComment(PeekTok); // Eat the ).
326 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000327 }
328 case tok::plus: {
329 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // Unary plus doesn't modify the value.
331 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000332 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
333 Result.setBegin(Start);
334 return false;
335 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 case tok::minus: {
337 SourceLocation Loc = PeekTok.getLocation();
338 PP.LexNonComment(PeekTok);
339 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000340 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000343 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattnerb081a352008-07-03 03:47:30 +0000345 // -MININT is the only thing that overflows. Unsigned never overflows.
346 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // If this operator is live and overflowed, report the issue.
349 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000350 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 DT.State = DefinedTracker::Unknown;
353 return false;
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner8ed30442008-05-05 06:45:50 +0000356 case tok::tilde: {
357 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 PP.LexNonComment(PeekTok);
359 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000360 Result.setBegin(Start);
361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000363 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 DT.State = DefinedTracker::Unknown;
365 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattner8ed30442008-05-05 06:45:50 +0000368 case tok::exclaim: {
369 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 PP.LexNonComment(PeekTok);
371 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000372 Result.setBegin(Start);
373 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000375 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 if (DT.State == DefinedTracker::DefinedMacro)
378 DT.State = DefinedTracker::NotDefinedMacro;
379 else if (DT.State == DefinedTracker::NotDefinedMacro)
380 DT.State = DefinedTracker::DefinedMacro;
381 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 // FIXME: Handle #assert
385 }
386}
387
388
389
390/// getPrecedence - Return the precedence of the specified binary operator
391/// token. This returns:
392/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000393/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000394/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000395static unsigned getPrecedence(tok::TokenKind Kind) {
396 switch (Kind) {
397 default: return ~0U;
398 case tok::percent:
399 case tok::slash:
400 case tok::star: return 14;
401 case tok::plus:
402 case tok::minus: return 13;
403 case tok::lessless:
404 case tok::greatergreater: return 12;
405 case tok::lessequal:
406 case tok::less:
407 case tok::greaterequal:
408 case tok::greater: return 11;
409 case tok::exclaimequal:
410 case tok::equalequal: return 10;
411 case tok::amp: return 9;
412 case tok::caret: return 8;
413 case tok::pipe: return 7;
414 case tok::ampamp: return 6;
415 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000416 case tok::question: return 4;
417 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000418 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000419 case tok::r_paren: return 0;// Lowest priority, end of expr.
420 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 }
422}
423
424
425/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000426/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000427///
428/// If ValueLive is false, then this value is being evaluated in a context where
429/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000430/// evaluation, such as division by zero warnings.
431static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000432 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 Preprocessor &PP) {
434 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
435 // If this token isn't valid, report the error.
436 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000437 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
438 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 return true;
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 while (1) {
443 // If this token has a lower precedence than we are allowed to parse, return
444 // it so that higher levels of the recursion can parse it.
445 if (PeekPrec < MinPrec)
446 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000451 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
453 // this example, the RHS of the && being dead does not make the rest of the
454 // expr dead.
455 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000456 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000458 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000460 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
462 else
463 RHSIsLive = ValueLive;
464
Chris Lattner8ed30442008-05-05 06:45:50 +0000465 // Consume the operator, remembering the operator's location for reporting.
466 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 PP.LexNonComment(PeekTok);
468
Chris Lattner8ed30442008-05-05 06:45:50 +0000469 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 // Parse the RHS of the operator.
471 DefinedTracker DT;
472 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
473
474 // Remember the precedence of this operator and get the precedence of the
475 // operator immediately to the right of the RHS.
476 unsigned ThisPrec = PeekPrec;
477 PeekPrec = getPrecedence(PeekTok.getKind());
478
479 // If this token isn't valid, report the error.
480 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000481 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
482 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 return true;
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner98ed49f2008-05-05 20:07:41 +0000486 // Decide whether to include the next binop in this subexpression. For
487 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000488 // handle y*z as a single subexpression. We do this because the precedence
489 // of * is higher than that of +. The only strange case we have to handle
490 // here is for the ?: operator, where the precedence is actually lower than
491 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000492 //
493 // conditional-expression ::=
494 // logical-OR-expression ? expression : conditional-expression
495 // where 'expression' is actually comma-expression.
496 unsigned RHSPrec;
497 if (Operator == tok::question)
498 // The RHS of "?" should be maximally consumed as an expression.
499 RHSPrec = getPrecedence(tok::comma);
500 else // All others should munch while higher precedence.
501 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner98ed49f2008-05-05 20:07:41 +0000503 if (PeekPrec >= RHSPrec) {
504 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 return true;
506 PeekPrec = getPrecedence(PeekTok.getKind());
507 }
508 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000511 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000513 switch (Operator) {
514 case tok::question: // No UAC for x and y in "x ? y : z".
515 case tok::lessless: // Shift amount doesn't UAC with shift value.
516 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
517 case tok::comma: // Comma operands are not subject to UACs.
518 case tok::pipepipe: // Logical || does not do UACs.
519 case tok::ampamp: // Logical && does not do UACs.
520 break; // No UAC
521 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
523 // If this just promoted something from signed to unsigned, and if the
524 // value was negative, warn about it.
525 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000526 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000527 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
528 << LHS.Val.toString(10, true) + " to " +
529 LHS.Val.toString(10, false)
530 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000531 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000532 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
533 << RHS.Val.toString(10, true) + " to " +
534 RHS.Val.toString(10, false)
535 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000537 LHS.Val.setIsUnsigned(Res.isUnsigned());
538 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 bool Overflow = false;
542 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000543 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000545 if (RHS.Val != 0)
546 Res = LHS.Val % RHS.Val;
547 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000548 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
549 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000550 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 break;
553 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000554 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000555 if (LHS.Val.isSigned())
556 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
557 else
558 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000559 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000560 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
561 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000562 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000567 if (Res.isSigned())
568 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
569 else
570 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 break;
572 case tok::lessless: {
573 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000574 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000575 if (LHS.isUnsigned()) {
576 Overflow = ShAmt >= LHS.Val.getBitWidth();
577 if (Overflow)
578 ShAmt = LHS.Val.getBitWidth()-1;
579 Res = LHS.Val << ShAmt;
580 } else {
581 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
582 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 break;
584 }
585 case tok::greatergreater: {
586 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000587 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 if (ShAmt >= LHS.getBitWidth())
589 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000590 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 break;
592 }
593 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000595 Res = LHS.Val + RHS.Val;
596 else
597 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 break;
599 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000601 Res = LHS.Val - RHS.Val;
602 else
603 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 break;
605 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000606 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
608 break;
609 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000610 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
612 break;
613 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000614 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
616 break;
617 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000618 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
620 break;
621 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000622 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
624 break;
625 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000626 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
628 break;
629 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000630 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 break;
632 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000633 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 break;
635 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000636 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 break;
638 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000639 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
641 break;
642 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000643 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
645 break;
646 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000647 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
648 // if not being evaluated.
649 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000650 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
651 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000652 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000653 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 case tok::question: {
655 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000656 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000657 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
658 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000659 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 return true;
661 }
662 // Consume the :.
663 PP.LexNonComment(PeekTok);
664
665 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000666 bool AfterColonLive = ValueLive && LHS.Val == 0;
667 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 DefinedTracker DT;
669 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
670 return true;
671
Chris Lattner44cbbb02008-05-05 20:09:27 +0000672 // Parse anything after the : with the same precedence as ?. We allow
673 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000674 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 PeekTok, AfterColonLive, PP))
676 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000679 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
680 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000681
682 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
683 // either operand is unsigned.
684 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Figure out the precedence of the token after the : part.
687 PeekPrec = getPrecedence(PeekTok.getKind());
688 break;
689 }
690 case tok::colon:
691 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000692 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
693 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 return true;
695 }
696
697 // If this operator is live and overflowed, report the issue.
698 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000699 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
700 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000703 LHS.Val = Res;
704 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 return false;
708}
709
710/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
711/// may occur after a #if or #elif directive. If the expression is equivalent
712/// to "!defined(X)" return X in IfNDefMacro.
713bool Preprocessor::
714EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000715 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
716 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
717 // in which case a directive is undefined behavior. We want macros to be able
718 // to recursively expand in order to get more gcc-list behavior, so we force
719 // DisableMacroExpansion to false and restore it when we're done parsing the
720 // expression.
721 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
722 DisableMacroExpansion = false;
723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000725 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000726 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000727
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000729 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattner8ed30442008-05-05 06:45:50 +0000731 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 DefinedTracker DT;
733 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
734 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000735 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000737
738 // Restore 'DisableMacroExpansion'.
739 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 return false;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // If we are at the end of the expression after just parsing a value, there
744 // must be no (unparenthesized) binary operators involved, so we can exit
745 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000746 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 // If the expression we parsed was of the form !defined(macro), return the
748 // macro in IfNDefMacro.
749 if (DT.State == DefinedTracker::NotDefinedMacro)
750 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattnera3e008a2009-12-14 05:00:18 +0000752 // Restore 'DisableMacroExpansion'.
753 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000754 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
758 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000759 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
760 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000762 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000764
765 // Restore 'DisableMacroExpansion'.
766 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 return false;
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Peter Collingbourne84021552011-02-28 02:37:51 +0000770 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000772 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 Diag(Tok, diag::err_pp_expected_eol);
774 DiscardUntilEndOfDirective();
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnera3e008a2009-12-14 05:00:18 +0000777 // Restore 'DisableMacroExpansion'.
778 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000779 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000780}