blob: 6975d31baba5255419fed3a46ed94871e0d366ca [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"
David Blaikieabc0bea2013-03-18 23:22:28 +000027#include "llvm/Support/SaveAndRestore.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Dan Gohman3c46e8d2010-07-26 21:25:24 +000030namespace {
31
Chris Lattner8ed30442008-05-05 06:45:50 +000032/// PPValue - Represents the value of a subexpression of a preprocessor
33/// conditional and the source range covered by it.
34class PPValue {
35 SourceRange Range;
36public:
37 llvm::APSInt Val;
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattner8ed30442008-05-05 06:45:50 +000039 // Default ctor - Construct an 'invalid' PPValue.
40 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump1eb44332009-09-09 15:08:12 +000041
Chris Lattner8ed30442008-05-05 06:45:50 +000042 unsigned getBitWidth() const { return Val.getBitWidth(); }
43 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattner8ed30442008-05-05 06:45:50 +000045 const SourceRange &getRange() const { return Range; }
Mike Stump1eb44332009-09-09 15:08:12 +000046
Chris Lattner8ed30442008-05-05 06:45:50 +000047 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
48 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump1eb44332009-09-09 15:08:12 +000049 Range.setBegin(B); Range.setEnd(E);
Chris Lattner8ed30442008-05-05 06:45:50 +000050 }
51 void setBegin(SourceLocation L) { Range.setBegin(L); }
52 void setEnd(SourceLocation L) { Range.setEnd(L); }
53};
54
Dan Gohman3c46e8d2010-07-26 21:25:24 +000055}
56
Chris Lattner8ed30442008-05-05 06:45:50 +000057static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +000058 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +000059 Preprocessor &PP);
60
61/// DefinedTracker - This struct is used while parsing expressions to keep track
62/// of whether !defined(X) has been seen.
63///
64/// With this simple scheme, we handle the basic forms:
65/// !defined(X) and !defined X
66/// but we also trivially handle (silly) stuff like:
67/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
68struct DefinedTracker {
69 /// Each time a Value is evaluated, it returns information about whether the
70 /// parsed value is of the form defined(X), !defined(X) or is something else.
71 enum TrackerState {
72 DefinedMacro, // defined(X)
73 NotDefinedMacro, // !defined(X)
74 Unknown // Something else.
75 } State;
76 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
77 /// indicates the macro that was checked.
78 IdentifierInfo *TheMacro;
79};
80
John Thompsona28cc092009-10-30 13:49:06 +000081/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattnera3e008a2009-12-14 05:00:18 +000082static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
83 bool ValueLive, Preprocessor &PP) {
John Thompsona28cc092009-10-30 13:49:06 +000084 IdentifierInfo *II;
John Thompson6fb63ab2013-07-19 18:50:04 +000085 SourceLocation beginLoc(PeekTok.getLocation());
86 Result.setBegin(beginLoc);
John Thompsona28cc092009-10-30 13:49:06 +000087
88 // Get the next token, don't expand it.
Eli Friedman88710f22011-08-03 00:04:13 +000089 PP.LexUnexpandedNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +000090
91 // Two options, it can either be a pp-identifier or a (.
92 SourceLocation LParenLoc;
93 if (PeekTok.is(tok::l_paren)) {
94 // Found a paren, remember we saw it and skip it.
95 LParenLoc = PeekTok.getLocation();
Eli Friedman88710f22011-08-03 00:04:13 +000096 PP.LexUnexpandedNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +000097 }
98
Douglas Gregor1fbb4472010-08-24 20:21:13 +000099 if (PeekTok.is(tok::code_completion)) {
100 if (PP.getCodeCompletionHandler())
101 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000102 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000103 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000104 }
105
John Thompsona28cc092009-10-30 13:49:06 +0000106 // If we don't have a pp-identifier now, this is an error.
107 if ((II = PeekTok.getIdentifierInfo()) == 0) {
108 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
109 return true;
110 }
111
112 // Otherwise, we got an identifier, is it defined to something?
113 Result.Val = II->hasMacroDefinition();
114 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
115
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000116 MacroDirective *Macro = 0;
John Thompsona28cc092009-10-30 13:49:06 +0000117 // If there is a macro, mark it used.
118 if (Result.Val != 0 && ValueLive) {
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000119 Macro = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +0000120 PP.markMacroAsUsed(Macro->getMacroInfo());
John Thompsona28cc092009-10-30 13:49:06 +0000121 }
122
John Thompson6fb63ab2013-07-19 18:50:04 +0000123 // Save macro token for callback.
124 Token macroToken(PeekTok);
Douglas Gregor3d5f9552011-10-14 00:49:43 +0000125
John Thompsona28cc092009-10-30 13:49:06 +0000126 // If we are in parens, ensure we have a trailing ).
127 if (LParenLoc.isValid()) {
Eli Friedman88710f22011-08-03 00:04:13 +0000128 // Consume identifier.
129 Result.setEnd(PeekTok.getLocation());
130 PP.LexUnexpandedNonComment(PeekTok);
131
John Thompsona28cc092009-10-30 13:49:06 +0000132 if (PeekTok.isNot(tok::r_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700133 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
134 << "'defined'" << tok::r_paren;
135 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsona28cc092009-10-30 13:49:06 +0000136 return true;
137 }
138 // Consume the ).
139 Result.setEnd(PeekTok.getLocation());
140 PP.LexNonComment(PeekTok);
Eli Friedman88710f22011-08-03 00:04:13 +0000141 } else {
142 // Consume identifier.
143 Result.setEnd(PeekTok.getLocation());
144 PP.LexNonComment(PeekTok);
John Thompsona28cc092009-10-30 13:49:06 +0000145 }
146
John Thompson6fb63ab2013-07-19 18:50:04 +0000147 // Invoke the 'defined' callback.
148 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
149 MacroDirective *MD = Macro;
150 // Pass the MacroInfo for the macro name even if the value is dead.
151 if (!MD && Result.Val != 0)
152 MD = PP.getMacroDirective(II);
153 Callbacks->Defined(macroToken, MD,
154 SourceRange(beginLoc, PeekTok.getLocation()));
155 }
156
John Thompsona28cc092009-10-30 13:49:06 +0000157 // Success, remember that we saw defined(X).
158 DT.State = DefinedTracker::DefinedMacro;
159 DT.TheMacro = II;
160 return false;
161}
162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
164/// return the computed value in Result. Return true if there was an error
165/// parsing. This function also returns information about the form of the
166/// expression in DT. See above for information on what DT means.
167///
168/// If ValueLive is false, then this value is being evaluated in a context where
169/// the result is not used. As such, avoid diagnostics that relate to
170/// evaluation.
Chris Lattner8ed30442008-05-05 06:45:50 +0000171static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
172 bool ValueLive, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 DT.State = DefinedTracker::Unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregorf29c5232010-08-24 22:20:20 +0000175 if (PeekTok.is(tok::code_completion)) {
176 if (PP.getCodeCompletionHandler())
177 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000178 PP.setCodeCompletionReached();
Eli Friedman88710f22011-08-03 00:04:13 +0000179 PP.LexNonComment(PeekTok);
Douglas Gregorf29c5232010-08-24 22:20:20 +0000180 }
181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 // If this token's spelling is a pp-identifier, check to see if it is
183 // 'defined' or if it is a macro. Note that we check here because many
184 // keywords are pp-identifiers, so we can't check the kind.
185 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattnerf8806622009-12-14 04:26:45 +0000186 // Handle "defined X" and "defined(X)".
187 if (II->isStr("defined"))
John Thompsona28cc092009-10-30 13:49:06 +0000188 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattnerf8806622009-12-14 04:26:45 +0000189
190 // If this identifier isn't 'defined' or one of the special
191 // preprocessor keywords and it wasn't macro expanded, it turns
192 // into a simple 0, unless it is the C++ keyword "true", in which case it
193 // turns into "1".
Eli Friedman9c611372012-09-20 02:38:38 +0000194 if (ValueLive &&
195 II->getTokenID() != tok::kw_true &&
196 II->getTokenID() != tok::kw_false)
Chris Lattnerf8806622009-12-14 04:26:45 +0000197 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
198 Result.Val = II->getTokenID() == tok::kw_true;
199 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
200 Result.setRange(PeekTok.getLocation());
201 PP.LexNonComment(PeekTok);
202 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 switch (PeekTok.getKind()) {
206 default: // Non-value token.
Chris Lattnerd98d9752008-04-13 20:38:43 +0000207 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 return true;
Peter Collingbourne84021552011-02-28 02:37:51 +0000209 case tok::eod:
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 case tok::r_paren:
211 // If there is no expression, report and exit.
212 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
213 return true;
214 case tok::numeric_constant: {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000215 SmallString<64> IntegerBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000216 bool NumberInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor50f6af72010-03-16 05:20:39 +0000218 &NumberInvalid);
219 if (NumberInvalid)
220 return true; // a diagnostic was already reported
221
Dmitri Gribenkofc97ea22012-09-24 09:53:54 +0000222 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 if (Literal.hadError)
224 return true; // a diagnostic was already reported.
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Chris Lattner6e400c22007-08-26 03:29:23 +0000226 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
228 return true;
229 }
230 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
231
Richard Smithb453ad32012-03-08 08:45:32 +0000232 // Complain about, and drop, any ud-suffix.
233 if (Literal.hasUDSuffix())
234 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
235
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000236 // 'long long' is a C99 or C++11 feature.
237 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
238 if (PP.getLangOpts().CPlusPlus)
239 PP.Diag(PeekTok,
Richard Smith80ad52f2013-01-02 11:42:31 +0000240 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenkoe3b136b2012-09-24 18:19:21 +0000241 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
242 else
243 PP.Diag(PeekTok, diag::ext_c99_longlong);
244 }
Neil Boothb9449512007-08-29 22:00:19 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // Parse the integer literal into Result.
Chris Lattner8ed30442008-05-05 06:45:50 +0000247 if (Literal.GetIntegerValue(Result.Val)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // Overflow parsing integer literal.
Eli Friedmanb3da6132013-07-23 00:25:18 +0000249 if (ValueLive) PP.Diag(PeekTok, diag::err_integer_too_large);
Chris Lattner8ed30442008-05-05 06:45:50 +0000250 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 } else {
252 // Set the signedness of the result to match whether there was a U suffix
253 // or not.
Chris Lattner8ed30442008-05-05 06:45:50 +0000254 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // Detect overflow based on whether the value is signed. If signed
257 // and if the value is too large, emit a warning "integer constant is so
258 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
259 // is 64-bits.
Chris Lattner8ed30442008-05-05 06:45:50 +0000260 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700261 // Octal, hexadecimal, and binary literals are implicitly unsigned if
262 // the value does not fit into a signed integer type.
Eli Friedmanb3da6132013-07-23 00:25:18 +0000263 if (ValueLive && Literal.getRadix() == 10)
Stephen Hines651f13c2014-04-23 16:59:28 -0700264 PP.Diag(PeekTok, diag::ext_integer_too_large_for_signed);
Chris Lattner8ed30442008-05-05 06:45:50 +0000265 Result.Val.setIsUnsigned(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 }
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000270 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 PP.LexNonComment(PeekTok);
272 return false;
273 }
Douglas Gregor5cee1192011-07-27 05:40:30 +0000274 case tok::char_constant: // 'x'
Alexander Kornienkod2267442013-01-31 19:03:16 +0000275 case tok::wide_char_constant: // L'x'
Douglas Gregor5cee1192011-07-27 05:40:30 +0000276 case tok::utf16_char_constant: // u'x'
Alexander Kornienkod2267442013-01-31 19:03:16 +0000277 case tok::utf32_char_constant: { // U'x'
Richard Smith99831e42012-03-06 03:21:47 +0000278 // Complain about, and drop, any ud-suffix.
279 if (PeekTok.hasUDSuffix())
Richard Smithb453ad32012-03-08 08:45:32 +0000280 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smith99831e42012-03-06 03:21:47 +0000281
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000282 SmallString<32> CharBuffer;
Douglas Gregor50f6af72010-03-16 05:20:39 +0000283 bool CharInvalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000285 if (CharInvalid)
286 return true;
Benjamin Kramerddeea562010-02-27 13:44:12 +0000287
288 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregor5cee1192011-07-27 05:40:30 +0000289 PeekTok.getLocation(), PP, PeekTok.getKind());
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 if (Literal.hadError())
291 return true; // A diagnostic was already emitted.
292
293 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar444be732009-11-13 05:51:54 +0000294 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000295 unsigned NumBits;
296 if (Literal.isMultiChar())
297 NumBits = TI.getIntWidth();
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000298 else if (Literal.isWide())
299 NumBits = TI.getWCharWidth();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000300 else if (Literal.isUTF16())
301 NumBits = TI.getChar16Width();
302 else if (Literal.isUTF32())
303 NumBits = TI.getChar32Width();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000304 else
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000305 NumBits = TI.getCharWidth();
Eli Friedman2a1c3632009-06-01 05:25:02 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Set the width.
308 llvm::APSInt Val(NumBits);
309 // Set the value.
310 Val = Literal.getValue();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000311 // Set the signedness. UTF-16 and UTF-32 are always unsigned
312 if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikie4e4d0842012-03-11 07:00:24 +0000313 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner8ed30442008-05-05 06:45:50 +0000315 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
316 Result.Val = Val.extend(Result.Val.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000318 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 "intmax_t smaller than char/wchar_t?");
Chris Lattner8ed30442008-05-05 06:45:50 +0000320 Result.Val = Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 }
322
323 // Consume the token.
Chris Lattner8ed30442008-05-05 06:45:50 +0000324 Result.setRange(PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 PP.LexNonComment(PeekTok);
326 return false;
327 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000328 case tok::l_paren: {
329 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 PP.LexNonComment(PeekTok); // Eat the (.
331 // Parse the value and if there are any binary operators involved, parse
332 // them.
333 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
334
335 // If this is a silly value like (X), which doesn't need parens, check for
336 // !(defined X).
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000337 if (PeekTok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // Just use DT unmodified as our result.
339 } else {
Chris Lattner8ed30442008-05-05 06:45:50 +0000340 // Otherwise, we have something like (x+y), and we consumed '(x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
342 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000344 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000345 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
346 << Result.getRange();
Stephen Hines651f13c2014-04-23 16:59:28 -0700347 PP.Diag(Start, diag::note_matching) << tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 return true;
349 }
350 DT.State = DefinedTracker::Unknown;
351 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000352 Result.setRange(Start, PeekTok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 PP.LexNonComment(PeekTok); // Eat the ).
354 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000355 }
356 case tok::plus: {
357 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 // Unary plus doesn't modify the value.
359 PP.LexNonComment(PeekTok);
Chris Lattner8ed30442008-05-05 06:45:50 +0000360 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
361 Result.setBegin(Start);
362 return false;
363 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 case tok::minus: {
365 SourceLocation Loc = PeekTok.getLocation();
366 PP.LexNonComment(PeekTok);
367 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000368 Result.setBegin(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000371 Result.Val = -Result.Val;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattnerb081a352008-07-03 03:47:30 +0000373 // -MININT is the only thing that overflows. Unsigned never overflows.
374 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // If this operator is live and overflowed, report the issue.
377 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000378 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 DT.State = DefinedTracker::Unknown;
381 return false;
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattner8ed30442008-05-05 06:45:50 +0000384 case tok::tilde: {
385 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 PP.LexNonComment(PeekTok);
387 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000388 Result.setBegin(Start);
389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner8ed30442008-05-05 06:45:50 +0000391 Result.Val = ~Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 DT.State = DefinedTracker::Unknown;
393 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattner8ed30442008-05-05 06:45:50 +0000396 case tok::exclaim: {
397 SourceLocation Start = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 PP.LexNonComment(PeekTok);
399 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner8ed30442008-05-05 06:45:50 +0000400 Result.setBegin(Start);
401 Result.Val = !Result.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner8ed30442008-05-05 06:45:50 +0000403 Result.Val.setIsUnsigned(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 if (DT.State == DefinedTracker::DefinedMacro)
406 DT.State = DefinedTracker::NotDefinedMacro;
407 else if (DT.State == DefinedTracker::NotDefinedMacro)
408 DT.State = DefinedTracker::DefinedMacro;
409 return false;
Chris Lattner8ed30442008-05-05 06:45:50 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 // FIXME: Handle #assert
413 }
414}
415
416
417
418/// getPrecedence - Return the precedence of the specified binary operator
419/// token. This returns:
420/// ~0 - Invalid token.
Chris Lattner9e66ba62008-05-05 04:10:51 +0000421/// 14 -> 3 - various operators.
Peter Collingbourne84021552011-02-28 02:37:51 +0000422/// 0 - 'eod' or ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000423static unsigned getPrecedence(tok::TokenKind Kind) {
424 switch (Kind) {
425 default: return ~0U;
426 case tok::percent:
427 case tok::slash:
428 case tok::star: return 14;
429 case tok::plus:
430 case tok::minus: return 13;
431 case tok::lessless:
432 case tok::greatergreater: return 12;
433 case tok::lessequal:
434 case tok::less:
435 case tok::greaterequal:
436 case tok::greater: return 11;
437 case tok::exclaimequal:
438 case tok::equalequal: return 10;
439 case tok::amp: return 9;
440 case tok::caret: return 8;
441 case tok::pipe: return 7;
442 case tok::ampamp: return 6;
443 case tok::pipepipe: return 5;
Chris Lattner98ed49f2008-05-05 20:07:41 +0000444 case tok::question: return 4;
445 case tok::comma: return 3;
Chris Lattner91891562008-05-04 18:36:18 +0000446 case tok::colon: return 2;
Peter Collingbourne84021552011-02-28 02:37:51 +0000447 case tok::r_paren: return 0;// Lowest priority, end of expr.
448 case tok::eod: return 0;// Lowest priority, end of directive.
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 }
450}
451
452
453/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner8ed30442008-05-05 06:45:50 +0000454/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Reid Spencer5f016e22007-07-11 17:01:13 +0000455///
456/// If ValueLive is false, then this value is being evaluated in a context where
457/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner8ed30442008-05-05 06:45:50 +0000458/// evaluation, such as division by zero warnings.
459static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattnerd2177732007-07-20 16:59:19 +0000460 Token &PeekTok, bool ValueLive,
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 Preprocessor &PP) {
462 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
463 // If this token isn't valid, report the error.
464 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000465 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
466 << LHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 return true;
468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 while (1) {
471 // If this token has a lower precedence than we are allowed to parse, return
472 // it so that higher levels of the recursion can parse it.
473 if (PeekPrec < MinPrec)
474 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump1eb44332009-09-09 15:08:12 +0000479 // dead. Note that this cannot just clobber ValueLive. Consider
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
481 // this example, the RHS of the && being dead does not make the rest of the
482 // expr dead.
483 bool RHSIsLive;
Chris Lattner8ed30442008-05-05 06:45:50 +0000484 if (Operator == tok::ampamp && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000486 else if (Operator == tok::pipepipe && LHS.Val != 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner8ed30442008-05-05 06:45:50 +0000488 else if (Operator == tok::question && LHS.Val == 0)
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
490 else
491 RHSIsLive = ValueLive;
492
Chris Lattner8ed30442008-05-05 06:45:50 +0000493 // Consume the operator, remembering the operator's location for reporting.
494 SourceLocation OpLoc = PeekTok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 PP.LexNonComment(PeekTok);
496
Chris Lattner8ed30442008-05-05 06:45:50 +0000497 PPValue RHS(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 // Parse the RHS of the operator.
499 DefinedTracker DT;
500 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
501
502 // Remember the precedence of this operator and get the precedence of the
503 // operator immediately to the right of the RHS.
504 unsigned ThisPrec = PeekPrec;
505 PeekPrec = getPrecedence(PeekTok.getKind());
506
507 // If this token isn't valid, report the error.
508 if (PeekPrec == ~0U) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000509 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
510 << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 return true;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner98ed49f2008-05-05 20:07:41 +0000514 // Decide whether to include the next binop in this subexpression. For
515 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattner44cbbb02008-05-05 20:09:27 +0000516 // handle y*z as a single subexpression. We do this because the precedence
517 // of * is higher than that of +. The only strange case we have to handle
518 // here is for the ?: operator, where the precedence is actually lower than
519 // the LHS of the '?'. The grammar rule is:
Chris Lattner98ed49f2008-05-05 20:07:41 +0000520 //
521 // conditional-expression ::=
522 // logical-OR-expression ? expression : conditional-expression
523 // where 'expression' is actually comma-expression.
524 unsigned RHSPrec;
525 if (Operator == tok::question)
526 // The RHS of "?" should be maximally consumed as an expression.
527 RHSPrec = getPrecedence(tok::comma);
528 else // All others should munch while higher precedence.
529 RHSPrec = ThisPrec+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Chris Lattner98ed49f2008-05-05 20:07:41 +0000531 if (PeekPrec >= RHSPrec) {
532 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 return true;
534 PeekPrec = getPrecedence(PeekTok.getKind());
535 }
536 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump1eb44332009-09-09 15:08:12 +0000539 // either operand is unsigned.
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattner019ef7e2008-05-04 23:46:17 +0000541 switch (Operator) {
542 case tok::question: // No UAC for x and y in "x ? y : z".
543 case tok::lessless: // Shift amount doesn't UAC with shift value.
544 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
545 case tok::comma: // Comma operands are not subject to UACs.
546 case tok::pipepipe: // Logical || does not do UACs.
547 case tok::ampamp: // Logical && does not do UACs.
548 break; // No UAC
549 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
551 // If this just promoted something from signed to unsigned, and if the
552 // value was negative, warn about it.
553 if (ValueLive && Res.isUnsigned()) {
Chris Lattner8ed30442008-05-05 06:45:50 +0000554 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000555 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
556 << LHS.Val.toString(10, true) + " to " +
557 LHS.Val.toString(10, false)
558 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000559 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattner204b2fe2008-11-18 21:48:13 +0000560 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
561 << RHS.Val.toString(10, true) + " to " +
562 RHS.Val.toString(10, false)
563 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 }
Chris Lattner8ed30442008-05-05 06:45:50 +0000565 LHS.Val.setIsUnsigned(Res.isUnsigned());
566 RHS.Val.setIsUnsigned(Res.isUnsigned());
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 bool Overflow = false;
570 switch (Operator) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000571 default: llvm_unreachable("Unknown operator token!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 case tok::percent:
Chris Lattner8ed30442008-05-05 06:45:50 +0000573 if (RHS.Val != 0)
574 Res = LHS.Val % RHS.Val;
575 else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000576 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
577 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000578 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 break;
581 case tok::slash:
Chris Lattner8ed30442008-05-05 06:45:50 +0000582 if (RHS.Val != 0) {
Chris Lattnerf9e77342010-10-13 23:46:56 +0000583 if (LHS.Val.isSigned())
584 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
585 else
586 Res = LHS.Val / RHS.Val;
Chris Lattner8ed30442008-05-05 06:45:50 +0000587 } else if (ValueLive) {
Chris Lattner204b2fe2008-11-18 21:48:13 +0000588 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
589 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000590 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 case tok::star:
Chris Lattnerf9e77342010-10-13 23:46:56 +0000595 if (Res.isSigned())
596 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
597 else
598 Res = LHS.Val * RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 break;
600 case tok::lessless: {
601 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000602 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattnerf9e77342010-10-13 23:46:56 +0000603 if (LHS.isUnsigned()) {
604 Overflow = ShAmt >= LHS.Val.getBitWidth();
605 if (Overflow)
606 ShAmt = LHS.Val.getBitWidth()-1;
607 Res = LHS.Val << ShAmt;
608 } else {
609 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
610 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 break;
612 }
613 case tok::greatergreater: {
614 // Determine whether overflow is about to happen.
Chris Lattner8ed30442008-05-05 06:45:50 +0000615 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 if (ShAmt >= LHS.getBitWidth())
617 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner8ed30442008-05-05 06:45:50 +0000618 Res = LHS.Val >> ShAmt;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 break;
620 }
621 case tok::plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000623 Res = LHS.Val + RHS.Val;
624 else
625 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 break;
627 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 if (LHS.isUnsigned())
Chris Lattnerf9e77342010-10-13 23:46:56 +0000629 Res = LHS.Val - RHS.Val;
630 else
631 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 break;
633 case tok::lessequal:
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::less:
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::greaterequal:
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.8p6, result is always int (signed)
644 break;
645 case tok::greater:
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.8p6, result is always int (signed)
648 break;
649 case tok::exclaimequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000650 Res = LHS.Val != RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
652 break;
653 case tok::equalequal:
Chris Lattner8ed30442008-05-05 06:45:50 +0000654 Res = LHS.Val == RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
656 break;
657 case tok::amp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000658 Res = LHS.Val & RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000659 break;
660 case tok::caret:
Chris Lattner8ed30442008-05-05 06:45:50 +0000661 Res = LHS.Val ^ RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 break;
663 case tok::pipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000664 Res = LHS.Val | RHS.Val;
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 break;
666 case tok::ampamp:
Chris Lattner8ed30442008-05-05 06:45:50 +0000667 Res = (LHS.Val != 0 && RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
669 break;
670 case tok::pipepipe:
Chris Lattner8ed30442008-05-05 06:45:50 +0000671 Res = (LHS.Val != 0 || RHS.Val != 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
673 break;
674 case tok::comma:
Chris Lattner91891562008-05-04 18:36:18 +0000675 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
676 // if not being evaluated.
David Blaikie4e4d0842012-03-11 07:00:24 +0000677 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000678 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
679 << LHS.getRange() << RHS.getRange();
Chris Lattner8ed30442008-05-05 06:45:50 +0000680 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump1eb44332009-09-09 15:08:12 +0000681 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 case tok::question: {
683 // Parse the : part of the expression.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000684 if (PeekTok.isNot(tok::colon)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700685 PP.Diag(PeekTok.getLocation(), diag::err_expected)
686 << tok::colon << LHS.getRange() << RHS.getRange();
687 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 return true;
689 }
690 // Consume the :.
691 PP.LexNonComment(PeekTok);
692
693 // Evaluate the value after the :.
Chris Lattner8ed30442008-05-05 06:45:50 +0000694 bool AfterColonLive = ValueLive && LHS.Val == 0;
695 PPValue AfterColonVal(LHS.getBitWidth());
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 DefinedTracker DT;
697 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
698 return true;
699
Chris Lattner44cbbb02008-05-05 20:09:27 +0000700 // Parse anything after the : with the same precedence as ?. We allow
701 // things of equal precedence because ?: is right associative.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000702 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 PeekTok, AfterColonLive, PP))
704 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner8ed30442008-05-05 06:45:50 +0000707 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
708 RHS.setEnd(AfterColonVal.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
710 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
711 // either operand is unsigned.
712 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 // Figure out the precedence of the token after the : part.
715 PeekPrec = getPrecedence(PeekTok.getKind());
716 break;
717 }
718 case tok::colon:
719 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattner204b2fe2008-11-18 21:48:13 +0000720 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
721 << LHS.getRange() << RHS.getRange();
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 return true;
723 }
724
725 // If this operator is live and overflowed, report the issue.
726 if (Overflow && ValueLive)
Chris Lattner204b2fe2008-11-18 21:48:13 +0000727 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
728 << LHS.getRange() << RHS.getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // Put the result back into 'LHS' for our next iteration.
Chris Lattner8ed30442008-05-05 06:45:50 +0000731 LHS.Val = Res;
732 LHS.setEnd(RHS.getRange().getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000734}
735
736/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
737/// may occur after a #if or #elif directive. If the expression is equivalent
738/// to "!defined(X)" return X in IfNDefMacro.
739bool Preprocessor::
740EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikieabc0bea2013-03-18 23:22:28 +0000741 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattnera3e008a2009-12-14 05:00:18 +0000742 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
743 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
744 // in which case a directive is undefined behavior. We want macros to be able
745 // to recursively expand in order to get more gcc-list behavior, so we force
746 // DisableMacroExpansion to false and restore it when we're done parsing the
747 // expression.
748 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
749 DisableMacroExpansion = false;
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 // Peek ahead one token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000752 Token Tok;
Eli Friedman88710f22011-08-03 00:04:13 +0000753 LexNonComment(Tok);
Douglas Gregor1fbb4472010-08-24 20:21:13 +0000754
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner98be4942008-03-05 18:54:05 +0000756 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner8ed30442008-05-05 06:45:50 +0000758 PPValue ResVal(BitWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 DefinedTracker DT;
760 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
761 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000762 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000764
765 // Restore 'DisableMacroExpansion'.
766 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 return false;
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 // If we are at the end of the expression after just parsing a value, there
771 // must be no (unparenthesized) binary operators involved, so we can exit
772 // directly.
Peter Collingbourne84021552011-02-28 02:37:51 +0000773 if (Tok.is(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 // If the expression we parsed was of the form !defined(macro), return the
775 // macro in IfNDefMacro.
776 if (DT.State == DefinedTracker::NotDefinedMacro)
777 IfNDefMacro = DT.TheMacro;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnera3e008a2009-12-14 05:00:18 +0000779 // Restore 'DisableMacroExpansion'.
780 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000781 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
785 // operator and the stuff after it.
Chris Lattner98ed49f2008-05-05 20:07:41 +0000786 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
787 Tok, true, *this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 // Parse error, skip the rest of the macro line.
Peter Collingbourne84021552011-02-28 02:37:51 +0000789 if (Tok.isNot(tok::eod))
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 DiscardUntilEndOfDirective();
Chris Lattnera3e008a2009-12-14 05:00:18 +0000791
792 // Restore 'DisableMacroExpansion'.
793 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 return false;
795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Peter Collingbourne84021552011-02-28 02:37:51 +0000797 // If we aren't at the tok::eod token, something bad happened, like an extra
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 // ')' token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000799 if (Tok.isNot(tok::eod)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 Diag(Tok, diag::err_pp_expected_eol);
801 DiscardUntilEndOfDirective();
802 }
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Chris Lattnera3e008a2009-12-14 05:00:18 +0000804 // Restore 'DisableMacroExpansion'.
805 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner8ed30442008-05-05 06:45:50 +0000806 return ResVal.Val != 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}