blob: 8d8fe316e7eb9eb441cdd117b48c1f82916819c7 [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: {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000200 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'
Richard Smith99831e42012-03-06 03:21:47 +0000254 // Complain about, and drop, any ud-suffix.
255 if (PeekTok.hasUDSuffix())
256 PP.Diag(PeekTok, diag::err_pp_invalid_char_udl);
257
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000258 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000259 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000260 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000261 if (CharInvalid)
262 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000263
264 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000265 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 if (Literal.hadError())
267 return true; // A diagnostic was already emitted.
268
269 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000270 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000271 unsigned NumBits;
272 if (Literal.isMultiChar())
273 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000274 else if (Literal.isWide())
275 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000276 else if (Literal.isUTF16())
277 NumBits = TI.getChar16Width();
278 else if (Literal.isUTF32())
279 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000280 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000281 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // Set the width.
284 llvm::APSInt Val(NumBits);
285 // Set the value.
286 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000287 // Set the signedness. UTF-16 and UTF-32 are always unsigned
288 if (!Literal.isUTF16() && !Literal.isUTF32())
289 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattner8ed30442008-05-05 06:45:50 +0000291 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
292 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000294 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000296 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 }
298
299 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000300 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 PP.LexNonComment(PeekTok);
302 return false;
303 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000304 case tok::l_paren: {
305 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 PP.LexNonComment(PeekTok); // Eat the (.
307 // Parse the value and if there are any binary operators involved, parse
308 // them.
309 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
310
311 // If this is a silly value like (X), which doesn't need parens, check for
312 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000313 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Just use DT unmodified as our result.
315 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000316 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
318 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000320 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000321 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
322 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000323 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 return true;
325 }
326 DT.State = DefinedTracker::Unknown;
327 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000328 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 PP.LexNonComment(PeekTok); // Eat the ).
330 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000331 }
332 case tok::plus: {
333 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Unary plus doesn't modify the value.
335 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000336 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
337 Result.setBegin(Start);
338 return false;
339 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 case tok::minus: {
341 SourceLocation Loc = PeekTok.getLocation();
342 PP.LexNonComment(PeekTok);
343 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000344 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000347 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattnerb081a352008-07-03 03:47:30 +0000349 // -MININT is the only thing that overflows. Unsigned never overflows.
350 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // If this operator is live and overflowed, report the issue.
353 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000354 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 DT.State = DefinedTracker::Unknown;
357 return false;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner8ed30442008-05-05 06:45:50 +0000360 case tok::tilde: {
361 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 PP.LexNonComment(PeekTok);
363 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000364 Result.setBegin(Start);
365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000367 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 DT.State = DefinedTracker::Unknown;
369 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner8ed30442008-05-05 06:45:50 +0000372 case tok::exclaim: {
373 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 PP.LexNonComment(PeekTok);
375 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000376 Result.setBegin(Start);
377 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000379 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 if (DT.State == DefinedTracker::DefinedMacro)
382 DT.State = DefinedTracker::NotDefinedMacro;
383 else if (DT.State == DefinedTracker::NotDefinedMacro)
384 DT.State = DefinedTracker::DefinedMacro;
385 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 // FIXME: Handle #assert
389 }
390}
391
392
393
394/// getPrecedence - Return the precedence of the specified binary operator
395/// token. This returns:
396/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000397/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000398/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000399static unsigned getPrecedence(tok::TokenKind Kind) {
400 switch (Kind) {
401 default: return ~0U;
402 case tok::percent:
403 case tok::slash:
404 case tok::star: return 14;
405 case tok::plus:
406 case tok::minus: return 13;
407 case tok::lessless:
408 case tok::greatergreater: return 12;
409 case tok::lessequal:
410 case tok::less:
411 case tok::greaterequal:
412 case tok::greater: return 11;
413 case tok::exclaimequal:
414 case tok::equalequal: return 10;
415 case tok::amp: return 9;
416 case tok::caret: return 8;
417 case tok::pipe: return 7;
418 case tok::ampamp: return 6;
419 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000420 case tok::question: return 4;
421 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000422 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000423 case tok::r_paren: return 0;// Lowest priority, end of expr.
424 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 }
426}
427
428
429/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000430/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000431///
432/// If ValueLive is false, then this value is being evaluated in a context where
433/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000434/// evaluation, such as division by zero warnings.
435static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000436 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 Preprocessor &PP) {
438 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
439 // If this token isn't valid, report the error.
440 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000441 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
442 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 return true;
444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 while (1) {
447 // If this token has a lower precedence than we are allowed to parse, return
448 // it so that higher levels of the recursion can parse it.
449 if (PeekPrec < MinPrec)
450 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000455 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
457 // this example, the RHS of the && being dead does not make the rest of the
458 // expr dead.
459 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000460 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000462 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000464 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
466 else
467 RHSIsLive = ValueLive;
468
Chris Lattner8ed30442008-05-05 06:45:50 +0000469 // Consume the operator, remembering the operator's location for reporting.
470 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 PP.LexNonComment(PeekTok);
472
Chris Lattner8ed30442008-05-05 06:45:50 +0000473 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 // Parse the RHS of the operator.
475 DefinedTracker DT;
476 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
477
478 // Remember the precedence of this operator and get the precedence of the
479 // operator immediately to the right of the RHS.
480 unsigned ThisPrec = PeekPrec;
481 PeekPrec = getPrecedence(PeekTok.getKind());
482
483 // If this token isn't valid, report the error.
484 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000485 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
486 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 return true;
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner98ed49f2008-05-05 20:07:41 +0000490 // Decide whether to include the next binop in this subexpression. For
491 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000492 // handle y*z as a single subexpression. We do this because the precedence
493 // of * is higher than that of +. The only strange case we have to handle
494 // here is for the ?: operator, where the precedence is actually lower than
495 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000496 //
497 // conditional-expression ::=
498 // logical-OR-expression ? expression : conditional-expression
499 // where 'expression' is actually comma-expression.
500 unsigned RHSPrec;
501 if (Operator == tok::question)
502 // The RHS of "?" should be maximally consumed as an expression.
503 RHSPrec = getPrecedence(tok::comma);
504 else // All others should munch while higher precedence.
505 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner98ed49f2008-05-05 20:07:41 +0000507 if (PeekPrec >= RHSPrec) {
508 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 return true;
510 PeekPrec = getPrecedence(PeekTok.getKind());
511 }
512 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000515 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000517 switch (Operator) {
518 case tok::question: // No UAC for x and y in "x ? y : z".
519 case tok::lessless: // Shift amount doesn't UAC with shift value.
520 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
521 case tok::comma: // Comma operands are not subject to UACs.
522 case tok::pipepipe: // Logical || does not do UACs.
523 case tok::ampamp: // Logical && does not do UACs.
524 break; // No UAC
525 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
527 // If this just promoted something from signed to unsigned, and if the
528 // value was negative, warn about it.
529 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000530 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000531 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
532 << LHS.Val.toString(10, true) + " to " +
533 LHS.Val.toString(10, false)
534 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000535 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000536 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
537 << RHS.Val.toString(10, true) + " to " +
538 RHS.Val.toString(10, false)
539 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000541 LHS.Val.setIsUnsigned(Res.isUnsigned());
542 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 bool Overflow = false;
546 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000547 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000549 if (RHS.Val != 0)
550 Res = LHS.Val % RHS.Val;
551 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000552 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
553 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000554 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 break;
557 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000558 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000559 if (LHS.Val.isSigned())
560 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
561 else
562 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000563 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000564 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
565 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000566 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000571 if (Res.isSigned())
572 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
573 else
574 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 break;
576 case tok::lessless: {
577 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000578 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000579 if (LHS.isUnsigned()) {
580 Overflow = ShAmt >= LHS.Val.getBitWidth();
581 if (Overflow)
582 ShAmt = LHS.Val.getBitWidth()-1;
583 Res = LHS.Val << ShAmt;
584 } else {
585 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
586 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 break;
588 }
589 case tok::greatergreater: {
590 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000591 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 if (ShAmt >= LHS.getBitWidth())
593 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000594 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 break;
596 }
597 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000599 Res = LHS.Val + RHS.Val;
600 else
601 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 break;
603 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000605 Res = LHS.Val - RHS.Val;
606 else
607 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 break;
609 case tok::lessequal:
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::less:
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::greaterequal:
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::greater:
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.8p6, result is always int (signed)
624 break;
625 case tok::exclaimequal:
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::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000630 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
632 break;
633 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000634 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 break;
636 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000637 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 break;
639 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000640 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 break;
642 case tok::ampamp:
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.13p3, result is always int (signed)
645 break;
646 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000647 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
649 break;
650 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000651 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
652 // if not being evaluated.
653 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000654 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
655 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000656 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000657 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 case tok::question: {
659 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000660 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000661 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
662 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000663 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 return true;
665 }
666 // Consume the :.
667 PP.LexNonComment(PeekTok);
668
669 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000670 bool AfterColonLive = ValueLive && LHS.Val == 0;
671 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 DefinedTracker DT;
673 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
674 return true;
675
Chris Lattner44cbbb02008-05-05 20:09:27 +0000676 // Parse anything after the : with the same precedence as ?. We allow
677 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000678 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 PeekTok, AfterColonLive, PP))
680 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000683 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
684 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000685
686 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
687 // either operand is unsigned.
688 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 // Figure out the precedence of the token after the : part.
691 PeekPrec = getPrecedence(PeekTok.getKind());
692 break;
693 }
694 case tok::colon:
695 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000696 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
697 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 return true;
699 }
700
701 // If this operator is live and overflowed, report the issue.
702 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000703 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
704 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000707 LHS.Val = Res;
708 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000710}
711
712/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
713/// may occur after a #if or #elif directive. If the expression is equivalent
714/// to "!defined(X)" return X in IfNDefMacro.
715bool Preprocessor::
716EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000717 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
718 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
719 // in which case a directive is undefined behavior. We want macros to be able
720 // to recursively expand in order to get more gcc-list behavior, so we force
721 // DisableMacroExpansion to false and restore it when we're done parsing the
722 // expression.
723 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
724 DisableMacroExpansion = false;
725
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000727 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000728 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000731 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattner8ed30442008-05-05 06:45:50 +0000733 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 DefinedTracker DT;
735 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
736 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000737 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000739
740 // Restore 'DisableMacroExpansion'.
741 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 return false;
743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 // If we are at the end of the expression after just parsing a value, there
746 // must be no (unparenthesized) binary operators involved, so we can exit
747 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000748 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 // If the expression we parsed was of the form !defined(macro), return the
750 // macro in IfNDefMacro.
751 if (DT.State == DefinedTracker::NotDefinedMacro)
752 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Chris Lattnera3e008a2009-12-14 05:00:18 +0000754 // Restore 'DisableMacroExpansion'.
755 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000756 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
760 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000761 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
762 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000764 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000766
767 // Restore 'DisableMacroExpansion'.
768 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 return false;
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Peter Collingbourne84021552011-02-28 02:37:51 +0000772 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000774 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 Diag(Tok, diag::err_pp_expected_eol);
776 DiscardUntilEndOfDirective();
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnera3e008a2009-12-14 05:00:18 +0000779 // Restore 'DisableMacroExpansion'.
780 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000781 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782}