blob: 49dfa198f638bd80ec3ab32ef4db7c7bf3529f76 [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
Benjamin Krameraeb863c2010-02-27 16:29:36 +0000209 NumericLiteralParser Literal(Spelling.begin(), Spelling.end(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 PeekTok.getLocation(), PP);
211 if (Literal.hadError)
212 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner6e400c22007-08-26 03:29:23 +0000214 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
216 return true;
217 }
218 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
219
Richard Smithb453ad32012-03-08 08:45:32 +0000220 // Complain about, and drop, any ud-suffix.
221 if (Literal.hasUDSuffix())
222 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
223
Neil Boothb9449512007-08-29 22:00:19 +0000224 // long long is a C99 feature.
David Blaikie4e4d0842012-03-11 07:00:24 +0000225 if (!PP.getLangOpts().C99 && Literal.isLongLong)
226 PP.Diag(PeekTok, PP.getLangOpts().CPlusPlus0x ?
Richard Smith661a9962011-10-15 01:18:56 +0000227 diag::warn_cxx98_compat_longlong : diag::ext_longlong);
Neil Boothb9449512007-08-29 22:00:19 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000230 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 // Overflow parsing integer literal.
232 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000233 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 } else {
235 // Set the signedness of the result to match whether there was a U suffix
236 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000237 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // Detect overflow based on whether the value is signed. If signed
240 // and if the value is too large, emit a warning "integer constant is so
241 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
242 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000243 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000244 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
245 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000246 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
247 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 }
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000252 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 PP.LexNonComment(PeekTok);
254 return false;
255 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000256 case tok::char_constant: // 'x'
257 case tok::wide_char_constant: { // L'x'
258 case tok::utf16_char_constant: // u'x'
259 case tok::utf32_char_constant: // U'x'
Richard Smith99831e42012-03-06 03:21:47 +0000260 // Complain about, and drop, any ud-suffix.
261 if (PeekTok.hasUDSuffix())
Richard Smithb453ad32012-03-08 08:45:32 +0000262 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smith99831e42012-03-06 03:21:47 +0000263
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000264 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000265 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000267 if (CharInvalid)
268 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000269
270 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000271 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 if (Literal.hadError())
273 return true; // A diagnostic was already emitted.
274
275 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000276 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000277 unsigned NumBits;
278 if (Literal.isMultiChar())
279 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000280 else if (Literal.isWide())
281 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000282 else if (Literal.isUTF16())
283 NumBits = TI.getChar16Width();
284 else if (Literal.isUTF32())
285 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000286 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000287 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 // Set the width.
290 llvm::APSInt Val(NumBits);
291 // Set the value.
292 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000293 // Set the signedness. UTF-16 and UTF-32 are always unsigned
294 if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikie4e4d0842012-03-11 07:00:24 +0000295 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner8ed30442008-05-05 06:45:50 +0000297 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
298 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000300 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000302 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 }
304
305 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000306 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 PP.LexNonComment(PeekTok);
308 return false;
309 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000310 case tok::l_paren: {
311 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 PP.LexNonComment(PeekTok); // Eat the (.
313 // Parse the value and if there are any binary operators involved, parse
314 // them.
315 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
316
317 // If this is a silly value like (X), which doesn't need parens, check for
318 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000319 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // Just use DT unmodified as our result.
321 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000322 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
324 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000326 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000327 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
328 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000329 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 return true;
331 }
332 DT.State = DefinedTracker::Unknown;
333 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000334 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 PP.LexNonComment(PeekTok); // Eat the ).
336 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000337 }
338 case tok::plus: {
339 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // Unary plus doesn't modify the value.
341 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000342 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
343 Result.setBegin(Start);
344 return false;
345 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 case tok::minus: {
347 SourceLocation Loc = PeekTok.getLocation();
348 PP.LexNonComment(PeekTok);
349 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000350 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000353 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattnerb081a352008-07-03 03:47:30 +0000355 // -MININT is the only thing that overflows. Unsigned never overflows.
356 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 // If this operator is live and overflowed, report the issue.
359 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000360 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 DT.State = DefinedTracker::Unknown;
363 return false;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattner8ed30442008-05-05 06:45:50 +0000366 case tok::tilde: {
367 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 PP.LexNonComment(PeekTok);
369 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000370 Result.setBegin(Start);
371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000373 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 DT.State = DefinedTracker::Unknown;
375 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner8ed30442008-05-05 06:45:50 +0000378 case tok::exclaim: {
379 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 PP.LexNonComment(PeekTok);
381 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000382 Result.setBegin(Start);
383 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000385 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 if (DT.State == DefinedTracker::DefinedMacro)
388 DT.State = DefinedTracker::NotDefinedMacro;
389 else if (DT.State == DefinedTracker::NotDefinedMacro)
390 DT.State = DefinedTracker::DefinedMacro;
391 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // FIXME: Handle #assert
395 }
396}
397
398
399
400/// getPrecedence - Return the precedence of the specified binary operator
401/// token. This returns:
402/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000403/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000404/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000405static unsigned getPrecedence(tok::TokenKind Kind) {
406 switch (Kind) {
407 default: return ~0U;
408 case tok::percent:
409 case tok::slash:
410 case tok::star: return 14;
411 case tok::plus:
412 case tok::minus: return 13;
413 case tok::lessless:
414 case tok::greatergreater: return 12;
415 case tok::lessequal:
416 case tok::less:
417 case tok::greaterequal:
418 case tok::greater: return 11;
419 case tok::exclaimequal:
420 case tok::equalequal: return 10;
421 case tok::amp: return 9;
422 case tok::caret: return 8;
423 case tok::pipe: return 7;
424 case tok::ampamp: return 6;
425 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000426 case tok::question: return 4;
427 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000428 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000429 case tok::r_paren: return 0;// Lowest priority, end of expr.
430 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 }
432}
433
434
435/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000436/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000437///
438/// If ValueLive is false, then this value is being evaluated in a context where
439/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000440/// evaluation, such as division by zero warnings.
441static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000442 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 Preprocessor &PP) {
444 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
445 // If this token isn't valid, report the error.
446 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000447 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
448 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 return true;
450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 while (1) {
453 // If this token has a lower precedence than we are allowed to parse, return
454 // it so that higher levels of the recursion can parse it.
455 if (PeekPrec < MinPrec)
456 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000461 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
463 // this example, the RHS of the && being dead does not make the rest of the
464 // expr dead.
465 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000466 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000468 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000470 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
472 else
473 RHSIsLive = ValueLive;
474
Chris Lattner8ed30442008-05-05 06:45:50 +0000475 // Consume the operator, remembering the operator's location for reporting.
476 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 PP.LexNonComment(PeekTok);
478
Chris Lattner8ed30442008-05-05 06:45:50 +0000479 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 // Parse the RHS of the operator.
481 DefinedTracker DT;
482 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
483
484 // Remember the precedence of this operator and get the precedence of the
485 // operator immediately to the right of the RHS.
486 unsigned ThisPrec = PeekPrec;
487 PeekPrec = getPrecedence(PeekTok.getKind());
488
489 // If this token isn't valid, report the error.
490 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000491 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
492 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 return true;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner98ed49f2008-05-05 20:07:41 +0000496 // Decide whether to include the next binop in this subexpression. For
497 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000498 // handle y*z as a single subexpression. We do this because the precedence
499 // of * is higher than that of +. The only strange case we have to handle
500 // here is for the ?: operator, where the precedence is actually lower than
501 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000502 //
503 // conditional-expression ::=
504 // logical-OR-expression ? expression : conditional-expression
505 // where 'expression' is actually comma-expression.
506 unsigned RHSPrec;
507 if (Operator == tok::question)
508 // The RHS of "?" should be maximally consumed as an expression.
509 RHSPrec = getPrecedence(tok::comma);
510 else // All others should munch while higher precedence.
511 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattner98ed49f2008-05-05 20:07:41 +0000513 if (PeekPrec >= RHSPrec) {
514 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 return true;
516 PeekPrec = getPrecedence(PeekTok.getKind());
517 }
518 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000521 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000523 switch (Operator) {
524 case tok::question: // No UAC for x and y in "x ? y : z".
525 case tok::lessless: // Shift amount doesn't UAC with shift value.
526 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
527 case tok::comma: // Comma operands are not subject to UACs.
528 case tok::pipepipe: // Logical || does not do UACs.
529 case tok::ampamp: // Logical && does not do UACs.
530 break; // No UAC
531 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
533 // If this just promoted something from signed to unsigned, and if the
534 // value was negative, warn about it.
535 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000536 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000537 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
538 << LHS.Val.toString(10, true) + " to " +
539 LHS.Val.toString(10, false)
540 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000541 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000542 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
543 << RHS.Val.toString(10, true) + " to " +
544 RHS.Val.toString(10, false)
545 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000547 LHS.Val.setIsUnsigned(Res.isUnsigned());
548 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 bool Overflow = false;
552 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000553 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000555 if (RHS.Val != 0)
556 Res = LHS.Val % RHS.Val;
557 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000558 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
559 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000560 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 break;
563 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000564 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000565 if (LHS.Val.isSigned())
566 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
567 else
568 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000569 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000570 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
571 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000572 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000577 if (Res.isSigned())
578 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
579 else
580 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 break;
582 case tok::lessless: {
583 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000584 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000585 if (LHS.isUnsigned()) {
586 Overflow = ShAmt >= LHS.Val.getBitWidth();
587 if (Overflow)
588 ShAmt = LHS.Val.getBitWidth()-1;
589 Res = LHS.Val << ShAmt;
590 } else {
591 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
592 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 break;
594 }
595 case tok::greatergreater: {
596 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000597 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 if (ShAmt >= LHS.getBitWidth())
599 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000600 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 break;
602 }
603 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000605 Res = LHS.Val + RHS.Val;
606 else
607 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 break;
609 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000611 Res = LHS.Val - RHS.Val;
612 else
613 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 break;
615 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000616 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
618 break;
619 case tok::less:
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::greaterequal:
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::greater:
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::exclaimequal:
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.9p3, result is always int (signed)
634 break;
635 case tok::equalequal:
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::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000640 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 break;
642 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000643 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 break;
645 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000646 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 break;
648 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000649 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
651 break;
652 case tok::pipepipe:
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.14p3, result is always int (signed)
655 break;
656 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000657 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
658 // if not being evaluated.
David Blaikie4e4d0842012-03-11 07:00:24 +0000659 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000660 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
661 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000662 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000663 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 case tok::question: {
665 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000666 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000667 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
668 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000669 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 return true;
671 }
672 // Consume the :.
673 PP.LexNonComment(PeekTok);
674
675 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000676 bool AfterColonLive = ValueLive && LHS.Val == 0;
677 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 DefinedTracker DT;
679 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
680 return true;
681
Chris Lattner44cbbb02008-05-05 20:09:27 +0000682 // Parse anything after the : with the same precedence as ?. We allow
683 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000684 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 PeekTok, AfterColonLive, PP))
686 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000689 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
690 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000691
692 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
693 // either operand is unsigned.
694 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 // Figure out the precedence of the token after the : part.
697 PeekPrec = getPrecedence(PeekTok.getKind());
698 break;
699 }
700 case tok::colon:
701 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000702 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
703 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 return true;
705 }
706
707 // If this operator is live and overflowed, report the issue.
708 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000709 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
710 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000713 LHS.Val = Res;
714 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000716}
717
718/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
719/// may occur after a #if or #elif directive. If the expression is equivalent
720/// to "!defined(X)" return X in IfNDefMacro.
721bool Preprocessor::
722EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000723 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
724 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
725 // in which case a directive is undefined behavior. We want macros to be able
726 // to recursively expand in order to get more gcc-list behavior, so we force
727 // DisableMacroExpansion to false and restore it when we're done parsing the
728 // expression.
729 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
730 DisableMacroExpansion = false;
731
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000733 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000734 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000737 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattner8ed30442008-05-05 06:45:50 +0000739 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 DefinedTracker DT;
741 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
742 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000743 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000745
746 // Restore 'DisableMacroExpansion'.
747 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 return false;
749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 // If we are at the end of the expression after just parsing a value, there
752 // must be no (unparenthesized) binary operators involved, so we can exit
753 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000754 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 // If the expression we parsed was of the form !defined(macro), return the
756 // macro in IfNDefMacro.
757 if (DT.State == DefinedTracker::NotDefinedMacro)
758 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattnera3e008a2009-12-14 05:00:18 +0000760 // Restore 'DisableMacroExpansion'.
761 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000762 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
766 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000767 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
768 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000770 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000772
773 // Restore 'DisableMacroExpansion'.
774 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 return false;
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Peter Collingbourne84021552011-02-28 02:37:51 +0000778 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000780 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 Diag(Tok, diag::err_pp_expected_eol);
782 DiscardUntilEndOfDirective();
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Chris Lattnera3e008a2009-12-14 05:00:18 +0000785 // Restore 'DisableMacroExpansion'.
786 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000787 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000788}