blob: ae2f5c1ab234b5200f729bc2c1b926f32c1a991e [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
Richard Smithb453ad32012-03-08 08:45:32 +0000218 // Complain about, and drop, any ud-suffix.
219 if (Literal.hasUDSuffix())
220 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
221
Neil Boothb9449512007-08-29 22:00:19 +0000222 // long long is a C99 feature.
Richard Smith661a9962011-10-15 01:18:56 +0000223 if (!PP.getLangOptions().C99 && Literal.isLongLong)
224 PP.Diag(PeekTok, PP.getLangOptions().CPlusPlus0x ?
225 diag::warn_cxx98_compat_longlong : diag::ext_longlong);
Neil Boothb9449512007-08-29 22:00:19 +0000226
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000228 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // Overflow parsing integer literal.
230 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000231 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 } else {
233 // Set the signedness of the result to match whether there was a U suffix
234 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000235 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Detect overflow based on whether the value is signed. If signed
238 // and if the value is too large, emit a warning "integer constant is so
239 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
240 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000241 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000242 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
243 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000244 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
245 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000250 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 PP.LexNonComment(PeekTok);
252 return false;
253 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000254 case tok::char_constant: // 'x'
255 case tok::wide_char_constant: { // L'x'
256 case tok::utf16_char_constant: // u'x'
257 case tok::utf32_char_constant: // U'x'
Richard Smith99831e42012-03-06 03:21:47 +0000258 // Complain about, and drop, any ud-suffix.
259 if (PeekTok.hasUDSuffix())
Richard Smithb453ad32012-03-08 08:45:32 +0000260 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smith99831e42012-03-06 03:21:47 +0000261
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000262 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000263 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000265 if (CharInvalid)
266 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000267
268 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000269 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 if (Literal.hadError())
271 return true; // A diagnostic was already emitted.
272
273 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000274 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000275 unsigned NumBits;
276 if (Literal.isMultiChar())
277 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000278 else if (Literal.isWide())
279 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000280 else if (Literal.isUTF16())
281 NumBits = TI.getChar16Width();
282 else if (Literal.isUTF32())
283 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000284 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000285 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 // Set the width.
288 llvm::APSInt Val(NumBits);
289 // Set the value.
290 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000291 // Set the signedness. UTF-16 and UTF-32 are always unsigned
292 if (!Literal.isUTF16() && !Literal.isUTF32())
293 Val.setIsUnsigned(!PP.getLangOptions().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner8ed30442008-05-05 06:45:50 +0000295 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
296 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000298 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000300 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 }
302
303 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000304 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 PP.LexNonComment(PeekTok);
306 return false;
307 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000308 case tok::l_paren: {
309 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 PP.LexNonComment(PeekTok); // Eat the (.
311 // Parse the value and if there are any binary operators involved, parse
312 // them.
313 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
314
315 // If this is a silly value like (X), which doesn't need parens, check for
316 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000317 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // Just use DT unmodified as our result.
319 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000320 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
322 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000324 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000325 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
326 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000327 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 return true;
329 }
330 DT.State = DefinedTracker::Unknown;
331 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000332 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 PP.LexNonComment(PeekTok); // Eat the ).
334 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000335 }
336 case tok::plus: {
337 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // Unary plus doesn't modify the value.
339 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000340 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
341 Result.setBegin(Start);
342 return false;
343 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 case tok::minus: {
345 SourceLocation Loc = PeekTok.getLocation();
346 PP.LexNonComment(PeekTok);
347 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000348 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000351 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattnerb081a352008-07-03 03:47:30 +0000353 // -MININT is the only thing that overflows. Unsigned never overflows.
354 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // If this operator is live and overflowed, report the issue.
357 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000358 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 DT.State = DefinedTracker::Unknown;
361 return false;
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattner8ed30442008-05-05 06:45:50 +0000364 case tok::tilde: {
365 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 PP.LexNonComment(PeekTok);
367 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000368 Result.setBegin(Start);
369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000371 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 DT.State = DefinedTracker::Unknown;
373 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner8ed30442008-05-05 06:45:50 +0000376 case tok::exclaim: {
377 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 PP.LexNonComment(PeekTok);
379 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000380 Result.setBegin(Start);
381 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000383 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 if (DT.State == DefinedTracker::DefinedMacro)
386 DT.State = DefinedTracker::NotDefinedMacro;
387 else if (DT.State == DefinedTracker::NotDefinedMacro)
388 DT.State = DefinedTracker::DefinedMacro;
389 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 // FIXME: Handle #assert
393 }
394}
395
396
397
398/// getPrecedence - Return the precedence of the specified binary operator
399/// token. This returns:
400/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000401/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000402/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000403static unsigned getPrecedence(tok::TokenKind Kind) {
404 switch (Kind) {
405 default: return ~0U;
406 case tok::percent:
407 case tok::slash:
408 case tok::star: return 14;
409 case tok::plus:
410 case tok::minus: return 13;
411 case tok::lessless:
412 case tok::greatergreater: return 12;
413 case tok::lessequal:
414 case tok::less:
415 case tok::greaterequal:
416 case tok::greater: return 11;
417 case tok::exclaimequal:
418 case tok::equalequal: return 10;
419 case tok::amp: return 9;
420 case tok::caret: return 8;
421 case tok::pipe: return 7;
422 case tok::ampamp: return 6;
423 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000424 case tok::question: return 4;
425 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000426 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000427 case tok::r_paren: return 0;// Lowest priority, end of expr.
428 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 }
430}
431
432
433/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000434/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000435///
436/// If ValueLive is false, then this value is being evaluated in a context where
437/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000438/// evaluation, such as division by zero warnings.
439static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000440 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 Preprocessor &PP) {
442 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
443 // If this token isn't valid, report the error.
444 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000445 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
446 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 return true;
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 while (1) {
451 // If this token has a lower precedence than we are allowed to parse, return
452 // it so that higher levels of the recursion can parse it.
453 if (PeekPrec < MinPrec)
454 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000459 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
461 // this example, the RHS of the && being dead does not make the rest of the
462 // expr dead.
463 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000464 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000466 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000468 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
470 else
471 RHSIsLive = ValueLive;
472
Chris Lattner8ed30442008-05-05 06:45:50 +0000473 // Consume the operator, remembering the operator's location for reporting.
474 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 PP.LexNonComment(PeekTok);
476
Chris Lattner8ed30442008-05-05 06:45:50 +0000477 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 // Parse the RHS of the operator.
479 DefinedTracker DT;
480 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
481
482 // Remember the precedence of this operator and get the precedence of the
483 // operator immediately to the right of the RHS.
484 unsigned ThisPrec = PeekPrec;
485 PeekPrec = getPrecedence(PeekTok.getKind());
486
487 // If this token isn't valid, report the error.
488 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000489 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
490 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 return true;
492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner98ed49f2008-05-05 20:07:41 +0000494 // Decide whether to include the next binop in this subexpression. For
495 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000496 // handle y*z as a single subexpression. We do this because the precedence
497 // of * is higher than that of +. The only strange case we have to handle
498 // here is for the ?: operator, where the precedence is actually lower than
499 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000500 //
501 // conditional-expression ::=
502 // logical-OR-expression ? expression : conditional-expression
503 // where 'expression' is actually comma-expression.
504 unsigned RHSPrec;
505 if (Operator == tok::question)
506 // The RHS of "?" should be maximally consumed as an expression.
507 RHSPrec = getPrecedence(tok::comma);
508 else // All others should munch while higher precedence.
509 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Chris Lattner98ed49f2008-05-05 20:07:41 +0000511 if (PeekPrec >= RHSPrec) {
512 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 return true;
514 PeekPrec = getPrecedence(PeekTok.getKind());
515 }
516 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000519 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000521 switch (Operator) {
522 case tok::question: // No UAC for x and y in "x ? y : z".
523 case tok::lessless: // Shift amount doesn't UAC with shift value.
524 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
525 case tok::comma: // Comma operands are not subject to UACs.
526 case tok::pipepipe: // Logical || does not do UACs.
527 case tok::ampamp: // Logical && does not do UACs.
528 break; // No UAC
529 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
531 // If this just promoted something from signed to unsigned, and if the
532 // value was negative, warn about it.
533 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000534 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000535 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
536 << LHS.Val.toString(10, true) + " to " +
537 LHS.Val.toString(10, false)
538 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000539 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000540 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
541 << RHS.Val.toString(10, true) + " to " +
542 RHS.Val.toString(10, false)
543 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000545 LHS.Val.setIsUnsigned(Res.isUnsigned());
546 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 bool Overflow = false;
550 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000551 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000553 if (RHS.Val != 0)
554 Res = LHS.Val % RHS.Val;
555 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000556 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
557 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000558 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 break;
561 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000562 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000563 if (LHS.Val.isSigned())
564 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
565 else
566 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000567 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000568 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
569 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000570 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000575 if (Res.isSigned())
576 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
577 else
578 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 break;
580 case tok::lessless: {
581 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000582 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000583 if (LHS.isUnsigned()) {
584 Overflow = ShAmt >= LHS.Val.getBitWidth();
585 if (Overflow)
586 ShAmt = LHS.Val.getBitWidth()-1;
587 Res = LHS.Val << ShAmt;
588 } else {
589 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
590 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 break;
592 }
593 case tok::greatergreater: {
594 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000595 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 if (ShAmt >= LHS.getBitWidth())
597 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000598 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 break;
600 }
601 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000603 Res = LHS.Val + RHS.Val;
604 else
605 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 break;
607 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000609 Res = LHS.Val - RHS.Val;
610 else
611 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 break;
613 case tok::lessequal:
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::less:
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::greaterequal:
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::greater:
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.8p6, result is always int (signed)
628 break;
629 case tok::exclaimequal:
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::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000634 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
636 break;
637 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000638 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 break;
640 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000641 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 break;
643 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000644 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 break;
646 case tok::ampamp:
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.13p3, result is always int (signed)
649 break;
650 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000651 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
653 break;
654 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000655 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
656 // if not being evaluated.
657 if (!PP.getLangOptions().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000658 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
659 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000660 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000661 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 case tok::question: {
663 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000664 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000665 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
666 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000667 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 return true;
669 }
670 // Consume the :.
671 PP.LexNonComment(PeekTok);
672
673 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000674 bool AfterColonLive = ValueLive && LHS.Val == 0;
675 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 DefinedTracker DT;
677 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
678 return true;
679
Chris Lattner44cbbb02008-05-05 20:09:27 +0000680 // Parse anything after the : with the same precedence as ?. We allow
681 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000682 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 PeekTok, AfterColonLive, PP))
684 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000687 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
688 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000689
690 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
691 // either operand is unsigned.
692 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Figure out the precedence of the token after the : part.
695 PeekPrec = getPrecedence(PeekTok.getKind());
696 break;
697 }
698 case tok::colon:
699 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000700 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
701 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 return true;
703 }
704
705 // If this operator is live and overflowed, report the issue.
706 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000707 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
708 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000711 LHS.Val = Res;
712 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000714}
715
716/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
717/// may occur after a #if or #elif directive. If the expression is equivalent
718/// to "!defined(X)" return X in IfNDefMacro.
719bool Preprocessor::
720EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000721 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
722 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
723 // in which case a directive is undefined behavior. We want macros to be able
724 // to recursively expand in order to get more gcc-list behavior, so we force
725 // DisableMacroExpansion to false and restore it when we're done parsing the
726 // expression.
727 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
728 DisableMacroExpansion = false;
729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000731 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000732 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000733
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000735 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Chris Lattner8ed30442008-05-05 06:45:50 +0000737 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 DefinedTracker DT;
739 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
740 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000741 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000743
744 // Restore 'DisableMacroExpansion'.
745 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 return false;
747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 // If we are at the end of the expression after just parsing a value, there
750 // must be no (unparenthesized) binary operators involved, so we can exit
751 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000752 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 // If the expression we parsed was of the form !defined(macro), return the
754 // macro in IfNDefMacro.
755 if (DT.State == DefinedTracker::NotDefinedMacro)
756 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattnera3e008a2009-12-14 05:00:18 +0000758 // Restore 'DisableMacroExpansion'.
759 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000760 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
764 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000765 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
766 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000768 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000770
771 // Restore 'DisableMacroExpansion'.
772 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 return false;
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Peter Collingbourne84021552011-02-28 02:37:51 +0000776 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000778 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 Diag(Tok, diag::err_pp_expected_eol);
780 DiscardUntilEndOfDirective();
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnera3e008a2009-12-14 05:00:18 +0000783 // Restore 'DisableMacroExpansion'.
784 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000785 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000786}