blob: f5984f58bf6f33cc1882ce3e30626b8101c95272 [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".
Eli Friedman9c611372012-09-20 02:38:38 +0000181 if (ValueLive &&
182 II->getTokenID() != tok::kw_true &&
183 II->getTokenID() != tok::kw_false)
Chris Lattnerf8806622009-12-14 04:26:45 +0000184 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
185 Result.Val = II->getTokenID() == tok::kw_true;
186 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
187 Result.setRange(PeekTok.getLocation());
188 PP.LexNonComment(PeekTok);
189 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 switch (PeekTok.getKind()) {
193 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000194 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000196 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 case tok::r_paren:
198 // If there is no expression, report and exit.
199 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
200 return true;
201 case tok::numeric_constant: {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000202 SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000203 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000205 &NumberInvalid);
206 if (NumberInvalid)
207 return true; // a diagnostic was already reported
208
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000209 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 if (Literal.hadError)
211 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Chris Lattner6e400c22007-08-26 03:29:23 +0000213 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
215 return true;
216 }
217 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
218
Richard Smithb453ad32012-03-08 08:45:32 +0000219 // Complain about, and drop, any ud-suffix.
220 if (Literal.hasUDSuffix())
221 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
222
Neil Boothb9449512007-08-29 22:00:19 +0000223 // long long is a C99 feature.
David Blaikie4e4d0842012-03-11 07:00:24 +0000224 if (!PP.getLangOpts().C99 && Literal.isLongLong)
225 PP.Diag(PeekTok, PP.getLangOpts().CPlusPlus0x ?
Richard Smith661a9962011-10-15 01:18:56 +0000226 diag::warn_cxx98_compat_longlong : diag::ext_longlong);
Neil Boothb9449512007-08-29 22:00:19 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000229 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // Overflow parsing integer literal.
231 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000232 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 } else {
234 // Set the signedness of the result to match whether there was a U suffix
235 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000236 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 // Detect overflow based on whether the value is signed. If signed
239 // and if the value is too large, emit a warning "integer constant is so
240 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
241 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000242 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000243 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
244 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000245 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
246 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 }
248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000251 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 PP.LexNonComment(PeekTok);
253 return false;
254 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000255 case tok::char_constant: // 'x'
256 case tok::wide_char_constant: { // L'x'
257 case tok::utf16_char_constant: // u'x'
258 case tok::utf32_char_constant: // U'x'
Richard Smith99831e42012-03-06 03:21:47 +0000259 // Complain about, and drop, any ud-suffix.
260 if (PeekTok.hasUDSuffix())
Richard Smithb453ad32012-03-08 08:45:32 +0000261 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smith99831e42012-03-06 03:21:47 +0000262
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000263 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000264 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000265 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000266 if (CharInvalid)
267 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000268
269 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000270 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 if (Literal.hadError())
272 return true; // A diagnostic was already emitted.
273
274 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000275 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000276 unsigned NumBits;
277 if (Literal.isMultiChar())
278 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000279 else if (Literal.isWide())
280 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000281 else if (Literal.isUTF16())
282 NumBits = TI.getChar16Width();
283 else if (Literal.isUTF32())
284 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000285 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000286 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // Set the width.
289 llvm::APSInt Val(NumBits);
290 // Set the value.
291 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000292 // Set the signedness. UTF-16 and UTF-32 are always unsigned
293 if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikie4e4d0842012-03-11 07:00:24 +0000294 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattner8ed30442008-05-05 06:45:50 +0000296 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
297 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000299 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000301 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 }
303
304 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000305 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 PP.LexNonComment(PeekTok);
307 return false;
308 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000309 case tok::l_paren: {
310 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 PP.LexNonComment(PeekTok); // Eat the (.
312 // Parse the value and if there are any binary operators involved, parse
313 // them.
314 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
315
316 // If this is a silly value like (X), which doesn't need parens, check for
317 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000318 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Just use DT unmodified as our result.
320 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000321 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
323 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000325 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000326 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
327 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000328 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 return true;
330 }
331 DT.State = DefinedTracker::Unknown;
332 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000333 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 PP.LexNonComment(PeekTok); // Eat the ).
335 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000336 }
337 case tok::plus: {
338 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 // Unary plus doesn't modify the value.
340 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000341 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
342 Result.setBegin(Start);
343 return false;
344 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 case tok::minus: {
346 SourceLocation Loc = PeekTok.getLocation();
347 PP.LexNonComment(PeekTok);
348 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000349 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000352 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattnerb081a352008-07-03 03:47:30 +0000354 // -MININT is the only thing that overflows. Unsigned never overflows.
355 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // If this operator is live and overflowed, report the issue.
358 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000359 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 DT.State = DefinedTracker::Unknown;
362 return false;
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner8ed30442008-05-05 06:45:50 +0000365 case tok::tilde: {
366 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 PP.LexNonComment(PeekTok);
368 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000369 Result.setBegin(Start);
370
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000372 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 DT.State = DefinedTracker::Unknown;
374 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner8ed30442008-05-05 06:45:50 +0000377 case tok::exclaim: {
378 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 PP.LexNonComment(PeekTok);
380 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000381 Result.setBegin(Start);
382 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000384 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 if (DT.State == DefinedTracker::DefinedMacro)
387 DT.State = DefinedTracker::NotDefinedMacro;
388 else if (DT.State == DefinedTracker::NotDefinedMacro)
389 DT.State = DefinedTracker::DefinedMacro;
390 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 // FIXME: Handle #assert
394 }
395}
396
397
398
399/// getPrecedence - Return the precedence of the specified binary operator
400/// token. This returns:
401/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000402/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000403/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000404static unsigned getPrecedence(tok::TokenKind Kind) {
405 switch (Kind) {
406 default: return ~0U;
407 case tok::percent:
408 case tok::slash:
409 case tok::star: return 14;
410 case tok::plus:
411 case tok::minus: return 13;
412 case tok::lessless:
413 case tok::greatergreater: return 12;
414 case tok::lessequal:
415 case tok::less:
416 case tok::greaterequal:
417 case tok::greater: return 11;
418 case tok::exclaimequal:
419 case tok::equalequal: return 10;
420 case tok::amp: return 9;
421 case tok::caret: return 8;
422 case tok::pipe: return 7;
423 case tok::ampamp: return 6;
424 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000425 case tok::question: return 4;
426 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000427 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000428 case tok::r_paren: return 0;// Lowest priority, end of expr.
429 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 }
431}
432
433
434/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000435/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000436///
437/// If ValueLive is false, then this value is being evaluated in a context where
438/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000439/// evaluation, such as division by zero warnings.
440static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000441 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 Preprocessor &PP) {
443 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
444 // If this token isn't valid, report the error.
445 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000446 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
447 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 return true;
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 while (1) {
452 // If this token has a lower precedence than we are allowed to parse, return
453 // it so that higher levels of the recursion can parse it.
454 if (PeekPrec < MinPrec)
455 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
462 // this example, the RHS of the && being dead does not make the rest of the
463 // expr dead.
464 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000465 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000467 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000469 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
471 else
472 RHSIsLive = ValueLive;
473
Chris Lattner8ed30442008-05-05 06:45:50 +0000474 // Consume the operator, remembering the operator's location for reporting.
475 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 PP.LexNonComment(PeekTok);
477
Chris Lattner8ed30442008-05-05 06:45:50 +0000478 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 // Parse the RHS of the operator.
480 DefinedTracker DT;
481 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
482
483 // Remember the precedence of this operator and get the precedence of the
484 // operator immediately to the right of the RHS.
485 unsigned ThisPrec = PeekPrec;
486 PeekPrec = getPrecedence(PeekTok.getKind());
487
488 // If this token isn't valid, report the error.
489 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000490 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
491 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 return true;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattner98ed49f2008-05-05 20:07:41 +0000495 // Decide whether to include the next binop in this subexpression. For
496 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000497 // handle y*z as a single subexpression. We do this because the precedence
498 // of * is higher than that of +. The only strange case we have to handle
499 // here is for the ?: operator, where the precedence is actually lower than
500 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000501 //
502 // conditional-expression ::=
503 // logical-OR-expression ? expression : conditional-expression
504 // where 'expression' is actually comma-expression.
505 unsigned RHSPrec;
506 if (Operator == tok::question)
507 // The RHS of "?" should be maximally consumed as an expression.
508 RHSPrec = getPrecedence(tok::comma);
509 else // All others should munch while higher precedence.
510 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattner98ed49f2008-05-05 20:07:41 +0000512 if (PeekPrec >= RHSPrec) {
513 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 return true;
515 PeekPrec = getPrecedence(PeekTok.getKind());
516 }
517 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000520 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000522 switch (Operator) {
523 case tok::question: // No UAC for x and y in "x ? y : z".
524 case tok::lessless: // Shift amount doesn't UAC with shift value.
525 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
526 case tok::comma: // Comma operands are not subject to UACs.
527 case tok::pipepipe: // Logical || does not do UACs.
528 case tok::ampamp: // Logical && does not do UACs.
529 break; // No UAC
530 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
532 // If this just promoted something from signed to unsigned, and if the
533 // value was negative, warn about it.
534 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000535 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000536 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
537 << LHS.Val.toString(10, true) + " to " +
538 LHS.Val.toString(10, false)
539 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000540 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000541 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
542 << RHS.Val.toString(10, true) + " to " +
543 RHS.Val.toString(10, false)
544 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000546 LHS.Val.setIsUnsigned(Res.isUnsigned());
547 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 bool Overflow = false;
551 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000552 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000554 if (RHS.Val != 0)
555 Res = LHS.Val % RHS.Val;
556 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000557 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
558 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000559 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 break;
562 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000563 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000564 if (LHS.Val.isSigned())
565 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
566 else
567 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000568 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000569 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
570 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000571 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000576 if (Res.isSigned())
577 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
578 else
579 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 break;
581 case tok::lessless: {
582 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000583 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000584 if (LHS.isUnsigned()) {
585 Overflow = ShAmt >= LHS.Val.getBitWidth();
586 if (Overflow)
587 ShAmt = LHS.Val.getBitWidth()-1;
588 Res = LHS.Val << ShAmt;
589 } else {
590 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
591 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 break;
593 }
594 case tok::greatergreater: {
595 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000596 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 if (ShAmt >= LHS.getBitWidth())
598 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000599 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 break;
601 }
602 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000604 Res = LHS.Val + RHS.Val;
605 else
606 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 break;
608 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000610 Res = LHS.Val - RHS.Val;
611 else
612 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 break;
614 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000615 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
617 break;
618 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000619 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
621 break;
622 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000623 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
625 break;
626 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000627 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
629 break;
630 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000631 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
633 break;
634 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000635 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
637 break;
638 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000639 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 break;
641 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000642 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 break;
644 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000645 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 break;
647 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000648 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
650 break;
651 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000652 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
654 break;
655 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000656 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
657 // if not being evaluated.
David Blaikie4e4d0842012-03-11 07:00:24 +0000658 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000659 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
660 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000661 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000662 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case tok::question: {
664 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000665 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000666 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
667 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000668 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 return true;
670 }
671 // Consume the :.
672 PP.LexNonComment(PeekTok);
673
674 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000675 bool AfterColonLive = ValueLive && LHS.Val == 0;
676 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 DefinedTracker DT;
678 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
679 return true;
680
Chris Lattner44cbbb02008-05-05 20:09:27 +0000681 // Parse anything after the : with the same precedence as ?. We allow
682 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000683 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 PeekTok, AfterColonLive, PP))
685 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000688 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
689 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
691 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
692 // either operand is unsigned.
693 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // Figure out the precedence of the token after the : part.
696 PeekPrec = getPrecedence(PeekTok.getKind());
697 break;
698 }
699 case tok::colon:
700 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000701 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
702 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 return true;
704 }
705
706 // If this operator is live and overflowed, report the issue.
707 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000708 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
709 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000712 LHS.Val = Res;
713 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000715}
716
717/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
718/// may occur after a #if or #elif directive. If the expression is equivalent
719/// to "!defined(X)" return X in IfNDefMacro.
720bool Preprocessor::
721EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000722 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
723 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
724 // in which case a directive is undefined behavior. We want macros to be able
725 // to recursively expand in order to get more gcc-list behavior, so we force
726 // DisableMacroExpansion to false and restore it when we're done parsing the
727 // expression.
728 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
729 DisableMacroExpansion = false;
730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000732 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000733 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000734
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000736 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner8ed30442008-05-05 06:45:50 +0000738 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 DefinedTracker DT;
740 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
741 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000742 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000744
745 // Restore 'DisableMacroExpansion'.
746 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 return false;
748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 // If we are at the end of the expression after just parsing a value, there
751 // must be no (unparenthesized) binary operators involved, so we can exit
752 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000753 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 // If the expression we parsed was of the form !defined(macro), return the
755 // macro in IfNDefMacro.
756 if (DT.State == DefinedTracker::NotDefinedMacro)
757 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattnera3e008a2009-12-14 05:00:18 +0000759 // Restore 'DisableMacroExpansion'.
760 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000761 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
765 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000766 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
767 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000769 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000771
772 // Restore 'DisableMacroExpansion'.
773 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 return false;
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Peter Collingbourne84021552011-02-28 02:37:51 +0000777 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000779 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 Diag(Tok, diag::err_pp_expected_eol);
781 DiscardUntilEndOfDirective();
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattnera3e008a2009-12-14 05:00:18 +0000784 // Restore 'DisableMacroExpansion'.
785 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000786 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000787}