blob: c564653e8f712ca5b9bf3ef72ec391c1aa67e979 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattner500d3292009-01-29 05:15:15 +000022#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/MacroInfo.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
Argyrios Kyrtzidis61c1c8e2012-12-08 02:21:11 +0000114 MacroInfo *Macro = 0;
John Thompsona28cc092009-10-30 13:49:06 +0000115 // If there is a macro, mark it used.
116 if (Result.Val != 0 && ValueLive) {
Argyrios Kyrtzidis61c1c8e2012-12-08 02:21:11 +0000117 Macro = PP.getMacroInfo(II);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000118 PP.markMacroAsUsed(Macro);
John Thompsona28cc092009-10-30 13:49:06 +0000119 }
120
Douglas Gregor3d5f9552011-10-14 00:49:43 +0000121 // Invoke the 'defined' callback.
Argyrios Kyrtzidis61c1c8e2012-12-08 02:21:11 +0000122 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
123 MacroInfo *MI = Macro;
124 // Pass the MacroInfo for the macro name even if the value is dead.
125 if (!MI && Result.Val != 0)
126 MI = PP.getMacroInfo(II);
127 Callbacks->Defined(PeekTok, MI);
128 }
Douglas Gregor3d5f9552011-10-14 00:49:43 +0000129
John Thompsona28cc092009-10-30 13:49:06 +0000130 // If we are in parens, ensure we have a trailing ).
131 if (LParenLoc.isValid()) {
Eli Friedman88710f22011-08-03 00:04:13 +0000132 // Consume identifier.
133 Result.setEnd(PeekTok.getLocation());
134 PP.LexUnexpandedNonComment(PeekTok);
135
John Thompsona28cc092009-10-30 13:49:06 +0000136 if (PeekTok.isNot(tok::r_paren)) {
137 PP.Diag(PeekTok.getLocation(), diag::err_pp_missing_rparen) << "defined";
138 PP.Diag(LParenLoc, diag::note_matching) << "(";
139 return true;
140 }
141 // Consume the ).
142 Result.setEnd(PeekTok.getLocation());
143 PP.LexNonComment(PeekTok);
Eli Friedman88710f22011-08-03 00:04:13 +0000144 } else {
145 // Consume identifier.
146 Result.setEnd(PeekTok.getLocation());
147 PP.LexNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000148 }
149
150 // Success, remember that we saw defined(X).
151 DT.State = DefinedTracker::DefinedMacro;
152 DT.TheMacro = II;
153 return false;
154}
155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
157/// return the computed value in Result. Return true if there was an error
158/// parsing. This function also returns information about the form of the
159/// expression in DT. See above for information on what DT means.
160///
161/// If ValueLive is false, then this value is being evaluated in a context where
162/// the result is not used. As such, avoid diagnostics that relate to
163/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000164static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
165 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregorf29c5232010-08-24 22:20:20 +0000168 if (PeekTok.is(tok::code_completion)) {
169 if (PP.getCodeCompletionHandler())
170 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000171 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000172 PP.LexNonComment(PeekTok);
Douglas Gregorf29c5232010-08-24 22:20:20 +0000173 }
174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // If this token's spelling is a pp-identifier, check to see if it is
176 // 'defined' or if it is a macro. Note that we check here because many
177 // keywords are pp-identifiers, so we can't check the kind.
178 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000179 // Handle "defined X" and "defined(X)".
180 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000181 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000182
183 // If this identifier isn't 'defined' or one of the special
184 // preprocessor keywords and it wasn't macro expanded, it turns
185 // into a simple 0, unless it is the C++ keyword "true", in which case it
186 // turns into "1".
Eli Friedman9c611372012-09-20 02:38:38 +0000187 if (ValueLive &&
188 II->getTokenID() != tok::kw_true &&
189 II->getTokenID() != tok::kw_false)
Chris Lattnerf8806622009-12-14 04:26:45 +0000190 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
191 Result.Val = II->getTokenID() == tok::kw_true;
192 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
193 Result.setRange(PeekTok.getLocation());
194 PP.LexNonComment(PeekTok);
195 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 switch (PeekTok.getKind()) {
199 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000200 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000202 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 case tok::r_paren:
204 // If there is no expression, report and exit.
205 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
206 return true;
207 case tok::numeric_constant: {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000208 SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000209 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000211 &NumberInvalid);
212 if (NumberInvalid)
213 return true; // a diagnostic was already reported
214
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000215 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 if (Literal.hadError)
217 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattner6e400c22007-08-26 03:29:23 +0000219 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
221 return true;
222 }
223 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
224
Richard Smithb453ad32012-03-08 08:45:32 +0000225 // Complain about, and drop, any ud-suffix.
226 if (Literal.hasUDSuffix())
227 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
228
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000229 // 'long long' is a C99 or C++11 feature.
230 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
231 if (PP.getLangOpts().CPlusPlus)
232 PP.Diag(PeekTok,
Richard Smith80ad52f2013-01-02 11:42:31 +0000233 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000234 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
235 else
236 PP.Diag(PeekTok, diag::ext_c99_longlong);
237 }
Neil Boothb9449512007-08-29 22:00:19 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000240 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // Overflow parsing integer literal.
242 if (ValueLive) PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000243 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 } else {
245 // Set the signedness of the result to match whether there was a U suffix
246 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000247 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 // Detect overflow based on whether the value is signed. If signed
250 // and if the value is too large, emit a warning "integer constant is so
251 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
252 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000253 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Chris Lattnerb081a352008-07-03 03:47:30 +0000254 // Don't warn for a hex literal: 0x8000..0 shouldn't warn.
255 if (ValueLive && Literal.getRadix() != 16)
Chris Lattner8ed30442008-05-05 06:45:50 +0000256 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
257 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 }
259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000262 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 PP.LexNonComment(PeekTok);
264 return false;
265 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000266 case tok::char_constant: // 'x'
267 case tok::wide_char_constant: { // L'x'
268 case tok::utf16_char_constant: // u'x'
269 case tok::utf32_char_constant: // U'x'
Richard Smith99831e42012-03-06 03:21:47 +0000270 // Complain about, and drop, any ud-suffix.
271 if (PeekTok.hasUDSuffix())
Richard Smithb453ad32012-03-08 08:45:32 +0000272 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smith99831e42012-03-06 03:21:47 +0000273
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000274 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000275 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000277 if (CharInvalid)
278 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000279
280 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000281 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 if (Literal.hadError())
283 return true; // A diagnostic was already emitted.
284
285 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000286 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000287 unsigned NumBits;
288 if (Literal.isMultiChar())
289 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000290 else if (Literal.isWide())
291 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000292 else if (Literal.isUTF16())
293 NumBits = TI.getChar16Width();
294 else if (Literal.isUTF32())
295 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000296 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000297 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // Set the width.
300 llvm::APSInt Val(NumBits);
301 // Set the value.
302 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000303 // Set the signedness. UTF-16 and UTF-32 are always unsigned
304 if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikie4e4d0842012-03-11 07:00:24 +0000305 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattner8ed30442008-05-05 06:45:50 +0000307 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
308 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000310 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000312 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 }
314
315 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000316 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 PP.LexNonComment(PeekTok);
318 return false;
319 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000320 case tok::l_paren: {
321 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 PP.LexNonComment(PeekTok); // Eat the (.
323 // Parse the value and if there are any binary operators involved, parse
324 // them.
325 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
326
327 // If this is a silly value like (X), which doesn't need parens, check for
328 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000329 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // Just use DT unmodified as our result.
331 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000332 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
334 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000336 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000337 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
338 << Result.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000339 PP.Diag(Start, diag::note_matching) << "(";
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 return true;
341 }
342 DT.State = DefinedTracker::Unknown;
343 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000344 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 PP.LexNonComment(PeekTok); // Eat the ).
346 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000347 }
348 case tok::plus: {
349 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 // Unary plus doesn't modify the value.
351 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000352 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
353 Result.setBegin(Start);
354 return false;
355 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 case tok::minus: {
357 SourceLocation Loc = PeekTok.getLocation();
358 PP.LexNonComment(PeekTok);
359 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000360 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000363 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattnerb081a352008-07-03 03:47:30 +0000365 // -MININT is the only thing that overflows. Unsigned never overflows.
366 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 // If this operator is live and overflowed, report the issue.
369 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000370 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 DT.State = DefinedTracker::Unknown;
373 return false;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner8ed30442008-05-05 06:45:50 +0000376 case tok::tilde: {
377 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 PP.LexNonComment(PeekTok);
379 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000380 Result.setBegin(Start);
381
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000383 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 DT.State = DefinedTracker::Unknown;
385 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattner8ed30442008-05-05 06:45:50 +0000388 case tok::exclaim: {
389 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 PP.LexNonComment(PeekTok);
391 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000392 Result.setBegin(Start);
393 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000395 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 if (DT.State == DefinedTracker::DefinedMacro)
398 DT.State = DefinedTracker::NotDefinedMacro;
399 else if (DT.State == DefinedTracker::NotDefinedMacro)
400 DT.State = DefinedTracker::DefinedMacro;
401 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 // FIXME: Handle #assert
405 }
406}
407
408
409
410/// getPrecedence - Return the precedence of the specified binary operator
411/// token. This returns:
412/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000413/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000414/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000415static unsigned getPrecedence(tok::TokenKind Kind) {
416 switch (Kind) {
417 default: return ~0U;
418 case tok::percent:
419 case tok::slash:
420 case tok::star: return 14;
421 case tok::plus:
422 case tok::minus: return 13;
423 case tok::lessless:
424 case tok::greatergreater: return 12;
425 case tok::lessequal:
426 case tok::less:
427 case tok::greaterequal:
428 case tok::greater: return 11;
429 case tok::exclaimequal:
430 case tok::equalequal: return 10;
431 case tok::amp: return 9;
432 case tok::caret: return 8;
433 case tok::pipe: return 7;
434 case tok::ampamp: return 6;
435 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000436 case tok::question: return 4;
437 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000438 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000439 case tok::r_paren: return 0;// Lowest priority, end of expr.
440 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 }
442}
443
444
445/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000446/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000447///
448/// If ValueLive is false, then this value is being evaluated in a context where
449/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000450/// evaluation, such as division by zero warnings.
451static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000452 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 Preprocessor &PP) {
454 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
455 // If this token isn't valid, report the error.
456 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000457 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
458 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 return true;
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 while (1) {
463 // If this token has a lower precedence than we are allowed to parse, return
464 // it so that higher levels of the recursion can parse it.
465 if (PeekPrec < MinPrec)
466 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000471 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
473 // this example, the RHS of the && being dead does not make the rest of the
474 // expr dead.
475 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000476 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000478 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000480 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
482 else
483 RHSIsLive = ValueLive;
484
Chris Lattner8ed30442008-05-05 06:45:50 +0000485 // Consume the operator, remembering the operator's location for reporting.
486 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 PP.LexNonComment(PeekTok);
488
Chris Lattner8ed30442008-05-05 06:45:50 +0000489 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 // Parse the RHS of the operator.
491 DefinedTracker DT;
492 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
493
494 // Remember the precedence of this operator and get the precedence of the
495 // operator immediately to the right of the RHS.
496 unsigned ThisPrec = PeekPrec;
497 PeekPrec = getPrecedence(PeekTok.getKind());
498
499 // If this token isn't valid, report the error.
500 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000501 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
502 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 return true;
504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattner98ed49f2008-05-05 20:07:41 +0000506 // Decide whether to include the next binop in this subexpression. For
507 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000508 // handle y*z as a single subexpression. We do this because the precedence
509 // of * is higher than that of +. The only strange case we have to handle
510 // here is for the ?: operator, where the precedence is actually lower than
511 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000512 //
513 // conditional-expression ::=
514 // logical-OR-expression ? expression : conditional-expression
515 // where 'expression' is actually comma-expression.
516 unsigned RHSPrec;
517 if (Operator == tok::question)
518 // The RHS of "?" should be maximally consumed as an expression.
519 RHSPrec = getPrecedence(tok::comma);
520 else // All others should munch while higher precedence.
521 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattner98ed49f2008-05-05 20:07:41 +0000523 if (PeekPrec >= RHSPrec) {
524 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 return true;
526 PeekPrec = getPrecedence(PeekTok.getKind());
527 }
528 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000531 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000533 switch (Operator) {
534 case tok::question: // No UAC for x and y in "x ? y : z".
535 case tok::lessless: // Shift amount doesn't UAC with shift value.
536 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
537 case tok::comma: // Comma operands are not subject to UACs.
538 case tok::pipepipe: // Logical || does not do UACs.
539 case tok::ampamp: // Logical && does not do UACs.
540 break; // No UAC
541 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
543 // If this just promoted something from signed to unsigned, and if the
544 // value was negative, warn about it.
545 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000546 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000547 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
548 << LHS.Val.toString(10, true) + " to " +
549 LHS.Val.toString(10, false)
550 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000551 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000552 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
553 << RHS.Val.toString(10, true) + " to " +
554 RHS.Val.toString(10, false)
555 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000557 LHS.Val.setIsUnsigned(Res.isUnsigned());
558 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 bool Overflow = false;
562 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000563 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000565 if (RHS.Val != 0)
566 Res = LHS.Val % RHS.Val;
567 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000568 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
569 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000570 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 break;
573 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000574 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000575 if (LHS.Val.isSigned())
576 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
577 else
578 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000579 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000580 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
581 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000582 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000587 if (Res.isSigned())
588 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
589 else
590 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 break;
592 case tok::lessless: {
593 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000594 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000595 if (LHS.isUnsigned()) {
596 Overflow = ShAmt >= LHS.Val.getBitWidth();
597 if (Overflow)
598 ShAmt = LHS.Val.getBitWidth()-1;
599 Res = LHS.Val << ShAmt;
600 } else {
601 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
602 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 break;
604 }
605 case tok::greatergreater: {
606 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000607 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 if (ShAmt >= LHS.getBitWidth())
609 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000610 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 break;
612 }
613 case tok::plus:
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.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 break;
619 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000621 Res = LHS.Val - RHS.Val;
622 else
623 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 break;
625 case tok::lessequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000626 Res = LHS.Val <= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
628 break;
629 case tok::less:
Chris Lattner8ed30442008-05-05 06:45:50 +0000630 Res = LHS.Val < RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
632 break;
633 case tok::greaterequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000634 Res = LHS.Val >= RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
636 break;
637 case tok::greater:
Chris Lattner8ed30442008-05-05 06:45:50 +0000638 Res = LHS.Val > RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
640 break;
641 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000642 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
644 break;
645 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000646 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
648 break;
649 case tok::amp:
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::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000653 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 break;
655 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000656 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 break;
658 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000659 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
661 break;
662 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000663 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
665 break;
666 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000667 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
668 // if not being evaluated.
David Blaikie4e4d0842012-03-11 07:00:24 +0000669 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000670 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
671 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000672 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000673 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 case tok::question: {
675 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000676 if (PeekTok.isNot(tok::colon)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000677 PP.Diag(PeekTok.getLocation(), diag::err_expected_colon)
678 << LHS.getRange(), RHS.getRange();
Chris Lattner28eb7e92008-11-23 23:17:07 +0000679 PP.Diag(OpLoc, diag::note_matching) << "?";
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 return true;
681 }
682 // Consume the :.
683 PP.LexNonComment(PeekTok);
684
685 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000686 bool AfterColonLive = ValueLive && LHS.Val == 0;
687 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 DefinedTracker DT;
689 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
690 return true;
691
Chris Lattner44cbbb02008-05-05 20:09:27 +0000692 // Parse anything after the : with the same precedence as ?. We allow
693 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000694 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 PeekTok, AfterColonLive, PP))
696 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000699 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
700 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000701
702 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
703 // either operand is unsigned.
704 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // Figure out the precedence of the token after the : part.
707 PeekPrec = getPrecedence(PeekTok.getKind());
708 break;
709 }
710 case tok::colon:
711 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000712 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
713 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 return true;
715 }
716
717 // If this operator is live and overflowed, report the issue.
718 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000719 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
720 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000723 LHS.Val = Res;
724 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000726}
727
728/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
729/// may occur after a #if or #elif directive. If the expression is equivalent
730/// to "!defined(X)" return X in IfNDefMacro.
731bool Preprocessor::
732EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
Chris Lattnera3e008a2009-12-14 05:00:18 +0000733 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
734 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
735 // in which case a directive is undefined behavior. We want macros to be able
736 // to recursively expand in order to get more gcc-list behavior, so we force
737 // DisableMacroExpansion to false and restore it when we're done parsing the
738 // expression.
739 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
740 DisableMacroExpansion = false;
741
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000743 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000744 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000745
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000747 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Chris Lattner8ed30442008-05-05 06:45:50 +0000749 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 DefinedTracker DT;
751 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
752 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000753 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000755
756 // Restore 'DisableMacroExpansion'.
757 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 return false;
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 // If we are at the end of the expression after just parsing a value, there
762 // must be no (unparenthesized) binary operators involved, so we can exit
763 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000764 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // If the expression we parsed was of the form !defined(macro), return the
766 // macro in IfNDefMacro.
767 if (DT.State == DefinedTracker::NotDefinedMacro)
768 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnera3e008a2009-12-14 05:00:18 +0000770 // Restore 'DisableMacroExpansion'.
771 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000772 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
776 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000777 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
778 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000779 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000780 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000782
783 // Restore 'DisableMacroExpansion'.
784 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 return false;
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Peter Collingbourne84021552011-02-28 02:37:51 +0000788 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000790 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 Diag(Tok, diag::err_pp_expected_eol);
792 DiscardUntilEndOfDirective();
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattnera3e008a2009-12-14 05:00:18 +0000795 // Restore 'DisableMacroExpansion'.
796 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000797 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000798}