blob: d5a88db470d8f47801ec5bef162017d5f320f395 [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
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000223 // 'long long' is a C99 or C++11 feature.
224 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
225 if (PP.getLangOpts().CPlusPlus)
226 PP.Diag(PeekTok,
227 PP.getLangOpts().CPlusPlus0x ?
228 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
229 else
230 PP.Diag(PeekTok, diag::ext_c99_longlong);
231 }
Neil Boothb9449512007-08-29 22:00:19 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000234 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // Overflow parsing integer literal.
236 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000237 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 } else {
239 // Set the signedness of the result to match whether there was a U suffix
240 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000241 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 // Detect overflow based on whether the value is signed. If signed
244 // and if the value is too large, emit a warning "integer constant is so
245 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
246 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000247 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000248 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
249 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000250 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
251 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 }
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000256 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 PP.LexNonComment(PeekTok);
258 return false;
259 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000260 case tok::char_constant: // 'x'
261 case tok::wide_char_constant: { // L'x'
262 case tok::utf16_char_constant: // u'x'
263 case tok::utf32_char_constant: // U'x'
Richard Smith99831e42012-03-06 03:21:47 +0000264 // Complain about, and drop, any ud-suffix.
265 if (PeekTok.hasUDSuffix())
Richard Smithb453ad32012-03-08 08:45:32 +0000266 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smith99831e42012-03-06 03:21:47 +0000267
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000268 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000269 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000270 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000271 if (CharInvalid)
272 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000273
274 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000275 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 if (Literal.hadError())
277 return true; // A diagnostic was already emitted.
278
279 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000280 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000281 unsigned NumBits;
282 if (Literal.isMultiChar())
283 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000284 else if (Literal.isWide())
285 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000286 else if (Literal.isUTF16())
287 NumBits = TI.getChar16Width();
288 else if (Literal.isUTF32())
289 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000290 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000291 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 // Set the width.
294 llvm::APSInt Val(NumBits);
295 // Set the value.
296 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000297 // Set the signedness. UTF-16 and UTF-32 are always unsigned
298 if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikie4e4d0842012-03-11 07:00:24 +0000299 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner8ed30442008-05-05 06:45:50 +0000301 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
302 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000304 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000306 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 }
308
309 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000310 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 PP.LexNonComment(PeekTok);
312 return false;
313 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000314 case tok::l_paren: {
315 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 PP.LexNonComment(PeekTok); // Eat the (.
317 // Parse the value and if there are any binary operators involved, parse
318 // them.
319 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
320
321 // If this is a silly value like (X), which doesn't need parens, check for
322 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000323 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // Just use DT unmodified as our result.
325 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000326 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
328 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000330 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000331 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
332 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000333 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 return true;
335 }
336 DT.State = DefinedTracker::Unknown;
337 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000338 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 PP.LexNonComment(PeekTok); // Eat the ).
340 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000341 }
342 case tok::plus: {
343 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // Unary plus doesn't modify the value.
345 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000346 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
347 Result.setBegin(Start);
348 return false;
349 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 case tok::minus: {
351 SourceLocation Loc = PeekTok.getLocation();
352 PP.LexNonComment(PeekTok);
353 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000354 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000357 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattnerb081a352008-07-03 03:47:30 +0000359 // -MININT is the only thing that overflows. Unsigned never overflows.
360 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // If this operator is live and overflowed, report the issue.
363 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000364 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 DT.State = DefinedTracker::Unknown;
367 return false;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattner8ed30442008-05-05 06:45:50 +0000370 case tok::tilde: {
371 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 PP.LexNonComment(PeekTok);
373 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000374 Result.setBegin(Start);
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000377 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 DT.State = DefinedTracker::Unknown;
379 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattner8ed30442008-05-05 06:45:50 +0000382 case tok::exclaim: {
383 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 PP.LexNonComment(PeekTok);
385 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000386 Result.setBegin(Start);
387 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000389 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 if (DT.State == DefinedTracker::DefinedMacro)
392 DT.State = DefinedTracker::NotDefinedMacro;
393 else if (DT.State == DefinedTracker::NotDefinedMacro)
394 DT.State = DefinedTracker::DefinedMacro;
395 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 // FIXME: Handle #assert
399 }
400}
401
402
403
404/// getPrecedence - Return the precedence of the specified binary operator
405/// token. This returns:
406/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000407/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000408/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000409static unsigned getPrecedence(tok::TokenKind Kind) {
410 switch (Kind) {
411 default: return ~0U;
412 case tok::percent:
413 case tok::slash:
414 case tok::star: return 14;
415 case tok::plus:
416 case tok::minus: return 13;
417 case tok::lessless:
418 case tok::greatergreater: return 12;
419 case tok::lessequal:
420 case tok::less:
421 case tok::greaterequal:
422 case tok::greater: return 11;
423 case tok::exclaimequal:
424 case tok::equalequal: return 10;
425 case tok::amp: return 9;
426 case tok::caret: return 8;
427 case tok::pipe: return 7;
428 case tok::ampamp: return 6;
429 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000430 case tok::question: return 4;
431 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000432 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000433 case tok::r_paren: return 0;// Lowest priority, end of expr.
434 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 }
436}
437
438
439/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000440/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000441///
442/// If ValueLive is false, then this value is being evaluated in a context where
443/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000444/// evaluation, such as division by zero warnings.
445static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000446 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 Preprocessor &PP) {
448 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
449 // If this token isn't valid, report the error.
450 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000451 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
452 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 return true;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 while (1) {
457 // If this token has a lower precedence than we are allowed to parse, return
458 // it so that higher levels of the recursion can parse it.
459 if (PeekPrec < MinPrec)
460 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000465 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
467 // this example, the RHS of the && being dead does not make the rest of the
468 // expr dead.
469 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000470 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000472 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000474 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
476 else
477 RHSIsLive = ValueLive;
478
Chris Lattner8ed30442008-05-05 06:45:50 +0000479 // Consume the operator, remembering the operator's location for reporting.
480 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 PP.LexNonComment(PeekTok);
482
Chris Lattner8ed30442008-05-05 06:45:50 +0000483 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 // Parse the RHS of the operator.
485 DefinedTracker DT;
486 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
487
488 // Remember the precedence of this operator and get the precedence of the
489 // operator immediately to the right of the RHS.
490 unsigned ThisPrec = PeekPrec;
491 PeekPrec = getPrecedence(PeekTok.getKind());
492
493 // If this token isn't valid, report the error.
494 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000495 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
496 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 return true;
498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattner98ed49f2008-05-05 20:07:41 +0000500 // Decide whether to include the next binop in this subexpression. For
501 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000502 // handle y*z as a single subexpression. We do this because the precedence
503 // of * is higher than that of +. The only strange case we have to handle
504 // here is for the ?: operator, where the precedence is actually lower than
505 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000506 //
507 // conditional-expression ::=
508 // logical-OR-expression ? expression : conditional-expression
509 // where 'expression' is actually comma-expression.
510 unsigned RHSPrec;
511 if (Operator == tok::question)
512 // The RHS of "?" should be maximally consumed as an expression.
513 RHSPrec = getPrecedence(tok::comma);
514 else // All others should munch while higher precedence.
515 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner98ed49f2008-05-05 20:07:41 +0000517 if (PeekPrec >= RHSPrec) {
518 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 return true;
520 PeekPrec = getPrecedence(PeekTok.getKind());
521 }
522 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000525 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000527 switch (Operator) {
528 case tok::question: // No UAC for x and y in "x ? y : z".
529 case tok::lessless: // Shift amount doesn't UAC with shift value.
530 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
531 case tok::comma: // Comma operands are not subject to UACs.
532 case tok::pipepipe: // Logical || does not do UACs.
533 case tok::ampamp: // Logical && does not do UACs.
534 break; // No UAC
535 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
537 // If this just promoted something from signed to unsigned, and if the
538 // value was negative, warn about it.
539 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000540 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000541 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
542 << LHS.Val.toString(10, true) + " to " +
543 LHS.Val.toString(10, false)
544 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000545 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000546 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
547 << RHS.Val.toString(10, true) + " to " +
548 RHS.Val.toString(10, false)
549 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000551 LHS.Val.setIsUnsigned(Res.isUnsigned());
552 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 bool Overflow = false;
556 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000557 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000559 if (RHS.Val != 0)
560 Res = LHS.Val % RHS.Val;
561 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000562 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
563 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000564 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 break;
567 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000568 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000569 if (LHS.Val.isSigned())
570 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
571 else
572 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000573 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000574 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
575 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000576 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000581 if (Res.isSigned())
582 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
583 else
584 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 break;
586 case tok::lessless: {
587 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000588 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000589 if (LHS.isUnsigned()) {
590 Overflow = ShAmt >= LHS.Val.getBitWidth();
591 if (Overflow)
592 ShAmt = LHS.Val.getBitWidth()-1;
593 Res = LHS.Val << ShAmt;
594 } else {
595 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
596 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 break;
598 }
599 case tok::greatergreater: {
600 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000601 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 if (ShAmt >= LHS.getBitWidth())
603 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000604 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 break;
606 }
607 case tok::plus:
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.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 break;
613 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000615 Res = LHS.Val - RHS.Val;
616 else
617 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 break;
619 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000620 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
622 break;
623 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000624 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
626 break;
627 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000628 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
630 break;
631 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000632 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
634 break;
635 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000636 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
638 break;
639 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000640 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
642 break;
643 case tok::amp:
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::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000647 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 break;
649 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000650 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 break;
652 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000653 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
655 break;
656 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000657 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
659 break;
660 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000661 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
662 // if not being evaluated.
David Blaikie4e4d0842012-03-11 07:00:24 +0000663 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000664 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
665 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000666 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000667 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 case tok::question: {
669 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000670 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000671 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
672 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000673 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 return true;
675 }
676 // Consume the :.
677 PP.LexNonComment(PeekTok);
678
679 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000680 bool AfterColonLive = ValueLive && LHS.Val == 0;
681 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 DefinedTracker DT;
683 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
684 return true;
685
Chris Lattner44cbbb02008-05-05 20:09:27 +0000686 // Parse anything after the : with the same precedence as ?. We allow
687 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000688 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 PeekTok, AfterColonLive, PP))
690 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000693 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
694 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000695
696 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
697 // either operand is unsigned.
698 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 // Figure out the precedence of the token after the : part.
701 PeekPrec = getPrecedence(PeekTok.getKind());
702 break;
703 }
704 case tok::colon:
705 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000706 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
707 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 return true;
709 }
710
711 // If this operator is live and overflowed, report the issue.
712 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000713 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
714 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000717 LHS.Val = Res;
718 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721
722/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
723/// may occur after a #if or #elif directive. If the expression is equivalent
724/// to "!defined(X)" return X in IfNDefMacro.
725bool Preprocessor::
726EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000727 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
728 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
729 // in which case a directive is undefined behavior. We want macros to be able
730 // to recursively expand in order to get more gcc-list behavior, so we force
731 // DisableMacroExpansion to false and restore it when we're done parsing the
732 // expression.
733 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
734 DisableMacroExpansion = false;
735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000737 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000738 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000741 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattner8ed30442008-05-05 06:45:50 +0000743 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 DefinedTracker DT;
745 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
746 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000747 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000749
750 // Restore 'DisableMacroExpansion'.
751 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 return false;
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 // If we are at the end of the expression after just parsing a value, there
756 // must be no (unparenthesized) binary operators involved, so we can exit
757 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000758 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 // If the expression we parsed was of the form !defined(macro), return the
760 // macro in IfNDefMacro.
761 if (DT.State == DefinedTracker::NotDefinedMacro)
762 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Chris Lattnera3e008a2009-12-14 05:00:18 +0000764 // Restore 'DisableMacroExpansion'.
765 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000766 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
770 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000771 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
772 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000774 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000776
777 // Restore 'DisableMacroExpansion'.
778 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 return false;
780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Peter Collingbourne84021552011-02-28 02:37:51 +0000782 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000784 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 Diag(Tok, diag::err_pp_expected_eol);
786 DiscardUntilEndOfDirective();
787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattnera3e008a2009-12-14 05:00:18 +0000789 // Restore 'DisableMacroExpansion'.
790 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000791 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000792}