blob: f7c3be99585ce4e0e746fe7029b7f6f06a46d953 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner80965422006-07-04 18:03:19 +000010// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14//
Chris Lattner6df79752007-04-04 06:54:19 +000015// FIXME: implement testing for #assert's.
Chris Lattner22eb9722006-06-18 05:43:12 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "clang/Lex/Preprocessor.h"
Chris Lattner81278c62006-10-14 19:03:49 +000020#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattner60f36222009-01-29 05:15:15 +000022#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/MacroInfo.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000025#include "llvm/ADT/APSInt.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000026#include "llvm/Support/ErrorHandling.h"
David Blaikie1dc4a3d2013-03-18 23:22:28 +000027#include "llvm/Support/SaveAndRestore.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000028using namespace clang;
29
Dan Gohman28ade552010-07-26 21:25:24 +000030namespace {
31
Chris Lattner3565c8e2008-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 Stump11289f42009-09-09 15:08:12 +000038
Chris Lattner3565c8e2008-05-05 06:45:50 +000039 // Default ctor - Construct an 'invalid' PPValue.
40 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump11289f42009-09-09 15:08:12 +000041
Chris Lattner3565c8e2008-05-05 06:45:50 +000042 unsigned getBitWidth() const { return Val.getBitWidth(); }
43 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump11289f42009-09-09 15:08:12 +000044
Chris Lattner3565c8e2008-05-05 06:45:50 +000045 const SourceRange &getRange() const { return Range; }
Mike Stump11289f42009-09-09 15:08:12 +000046
Chris Lattner3565c8e2008-05-05 06:45:50 +000047 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
48 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump11289f42009-09-09 15:08:12 +000049 Range.setBegin(B); Range.setEnd(E);
Chris Lattner3565c8e2008-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 Gohman28ade552010-07-26 21:25:24 +000055}
56
Chris Lattner3565c8e2008-05-05 06:45:50 +000057static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +000058 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +000059 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000060
Chris Lattnerb9d90f72006-07-04 18:32:03 +000061/// 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 Thompsonb5353522009-10-30 13:49:06 +000081/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattner4c53c402009-12-14 05:00:18 +000082static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
83 bool ValueLive, Preprocessor &PP) {
John Thompsonb5353522009-10-30 13:49:06 +000084 IdentifierInfo *II;
John Thompsoncda95fe2013-07-19 18:50:04 +000085 SourceLocation beginLoc(PeekTok.getLocation());
86 Result.setBegin(beginLoc);
John Thompsonb5353522009-10-30 13:49:06 +000087
88 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +000089 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-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 Friedmanb3bfd842011-08-03 00:04:13 +000096 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +000097 }
98
Douglas Gregor12785102010-08-24 20:21:13 +000099 if (PeekTok.is(tok::code_completion)) {
100 if (PP.getCodeCompletionHandler())
101 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000102 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000103 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000104 }
105
John Thompsonb5353522009-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 Kyrtzidisfead64b2013-02-24 00:05:14 +0000116 MacroDirective *Macro = 0;
John Thompsonb5353522009-10-30 13:49:06 +0000117 // If there is a macro, mark it used.
118 if (Result.Val != 0 && ValueLive) {
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000119 Macro = PP.getMacroDirective(II);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000120 PP.markMacroAsUsed(Macro->getMacroInfo());
John Thompsonb5353522009-10-30 13:49:06 +0000121 }
122
John Thompsoncda95fe2013-07-19 18:50:04 +0000123 // Save macro token for callback.
124 Token macroToken(PeekTok);
Douglas Gregor82e0d7282011-10-14 00:49:43 +0000125
John Thompsonb5353522009-10-30 13:49:06 +0000126 // If we are in parens, ensure we have a trailing ).
127 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000128 // Consume identifier.
129 Result.setEnd(PeekTok.getLocation());
130 PP.LexUnexpandedNonComment(PeekTok);
131
John Thompsonb5353522009-10-30 13:49:06 +0000132 if (PeekTok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +0000133 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
134 << "'defined'" << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +0000135 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsonb5353522009-10-30 13:49:06 +0000136 return true;
137 }
138 // Consume the ).
139 Result.setEnd(PeekTok.getLocation());
140 PP.LexNonComment(PeekTok);
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000141 } else {
142 // Consume identifier.
143 Result.setEnd(PeekTok.getLocation());
144 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000145 }
146
John Thompsoncda95fe2013-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 Thompsonb5353522009-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
Chris Lattner22eb9722006-06-18 05:43:12 +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
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000165/// parsing. This function also returns information about the form of the
166/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000167///
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 Lattner3565c8e2008-05-05 06:45:50 +0000171static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
172 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000173 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Douglas Gregorec00a262010-08-24 22:20:20 +0000175 if (PeekTok.is(tok::code_completion)) {
176 if (PP.getCodeCompletionHandler())
177 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000178 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000179 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000180 }
181
Chris Lattner22eb9722006-06-18 05:43:12 +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.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000185 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner676268e2009-12-14 04:26:45 +0000186 // Handle "defined X" and "defined(X)".
187 if (II->isStr("defined"))
John Thompsonb5353522009-10-30 13:49:06 +0000188 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattner676268e2009-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 Friedman847f3ca2012-09-20 02:38:38 +0000194 if (ValueLive &&
195 II->getTokenID() != tok::kw_true &&
196 II->getTokenID() != tok::kw_false)
Chris Lattner676268e2009-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;
Chris Lattner22eb9722006-06-18 05:43:12 +0000203 }
Mike Stump11289f42009-09-09 15:08:12 +0000204
Chris Lattner22eb9722006-06-18 05:43:12 +0000205 switch (PeekTok.getKind()) {
206 default: // Non-value token.
Chris Lattnerf8f94542008-04-13 20:38:43 +0000207 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000208 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000209 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000210 case tok::r_paren:
211 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000212 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000213 return true;
214 case tok::numeric_constant: {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000215 SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000216 bool NumberInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000217 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000218 &NumberInvalid);
219 if (NumberInvalid)
220 return true; // a diagnostic was already reported
221
Dmitri Gribenko7ba91722012-09-24 09:53:54 +0000222 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000223 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000224 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattnered045422007-08-26 03:29:23 +0000226 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000227 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000228 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000229 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000230 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000231
Richard Smith39570d002012-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 Gribenko1cd23052012-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 Smith2bf7fdb2013-01-02 11:42:31 +0000240 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenko1cd23052012-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 Boothac582c52007-08-29 22:00:19 +0000245
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000246 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000247 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000248 // Overflow parsing integer literal.
Eli Friedman088d39a2013-07-23 00:25:18 +0000249 if (ValueLive) PP.Diag(PeekTok, diag::err_integer_too_large);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000250 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000251 } else {
252 // Set the signedness of the result to match whether there was a U suffix
253 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000254 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000255
Chris Lattnera9eac7f2007-04-05 05:24:00 +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 Lattner3565c8e2008-05-05 06:45:50 +0000260 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Eli Friedman088d39a2013-07-23 00:25:18 +0000261 // Don't warn for a hex or octal literal: 0x8000..0 shouldn't warn.
262 if (ValueLive && Literal.getRadix() == 10)
Eli Friedmanab091872013-07-26 00:06:45 +0000263 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000264 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000265 }
266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000268 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000269 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000270 PP.LexNonComment(PeekTok);
271 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000272 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000273 case tok::char_constant: // 'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000274 case tok::wide_char_constant: // L'x'
Douglas Gregorfb65e592011-07-27 05:40:30 +0000275 case tok::utf16_char_constant: // u'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000276 case tok::utf32_char_constant: { // U'x'
Richard Smithd67aea22012-03-06 03:21:47 +0000277 // Complain about, and drop, any ud-suffix.
278 if (PeekTok.hasUDSuffix())
Richard Smith39570d002012-03-08 08:45:32 +0000279 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smithd67aea22012-03-06 03:21:47 +0000280
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000281 SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000282 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000283 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000284 if (CharInvalid)
285 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000286
287 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000288 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000289 if (Literal.hadError())
290 return true; // A diagnostic was already emitted.
291
292 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000293 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000294 unsigned NumBits;
295 if (Literal.isMultiChar())
296 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000297 else if (Literal.isWide())
298 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000299 else if (Literal.isUTF16())
300 NumBits = TI.getChar16Width();
301 else if (Literal.isUTF32())
302 NumBits = TI.getChar32Width();
Eli Friedmand8cec572009-06-01 05:25:02 +0000303 else
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000304 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000305
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000306 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000307 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000308 // Set the value.
309 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000310 // Set the signedness. UTF-16 and UTF-32 are always unsigned
311 if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikiebbafb8a2012-03-11 07:00:24 +0000312 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000313
Chris Lattner3565c8e2008-05-05 06:45:50 +0000314 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
315 Result.Val = Val.extend(Result.Val.getBitWidth());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000316 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000317 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000318 "intmax_t smaller than char/wchar_t?");
Chris Lattner3565c8e2008-05-05 06:45:50 +0000319 Result.Val = Val;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000320 }
321
322 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000323 Result.setRange(PeekTok.getLocation());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000324 PP.LexNonComment(PeekTok);
325 return false;
326 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000327 case tok::l_paren: {
328 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000329 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000330 // Parse the value and if there are any binary operators involved, parse
331 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000332 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000333
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000334 // If this is a silly value like (X), which doesn't need parens, check for
335 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000336 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000337 // Just use DT unmodified as our result.
338 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000339 // Otherwise, we have something like (x+y), and we consumed '(x'.
Chris Lattner86054a92007-04-10 05:26:38 +0000340 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
341 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000342
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000343 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000344 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
345 << Result.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000346 PP.Diag(Start, diag::note_matching) << tok::l_paren;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000347 return true;
348 }
349 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000350 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000351 Result.setRange(Start, PeekTok.getLocation());
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000352 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000353 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000354 }
355 case tok::plus: {
356 SourceLocation Start = PeekTok.getLocation();
Chris Lattner22eb9722006-06-18 05:43:12 +0000357 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000358 PP.LexNonComment(PeekTok);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000359 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
360 Result.setBegin(Start);
361 return false;
362 }
Chris Lattner7e61ac52007-04-11 03:42:36 +0000363 case tok::minus: {
364 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000365 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000366 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000367 Result.setBegin(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000369 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000370 Result.Val = -Result.Val;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattner1cb0e612008-07-03 03:47:30 +0000372 // -MININT is the only thing that overflows. Unsigned never overflows.
373 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattner7e61ac52007-04-11 03:42:36 +0000375 // If this operator is live and overflowed, report the issue.
376 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000377 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000379 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000380 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner3565c8e2008-05-05 06:45:50 +0000383 case tok::tilde: {
384 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000385 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000386 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000387 Result.setBegin(Start);
388
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000389 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000390 Result.Val = ~Result.Val;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000391 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000392 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattner3565c8e2008-05-05 06:45:50 +0000395 case tok::exclaim: {
396 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000397 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000398 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000399 Result.setBegin(Start);
400 Result.Val = !Result.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000401 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000402 Result.Val.setIsUnsigned(false);
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000404 if (DT.State == DefinedTracker::DefinedMacro)
405 DT.State = DefinedTracker::NotDefinedMacro;
406 else if (DT.State == DefinedTracker::NotDefinedMacro)
407 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000408 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000409 }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner22eb9722006-06-18 05:43:12 +0000411 // FIXME: Handle #assert
412 }
413}
414
415
416
417/// getPrecedence - Return the precedence of the specified binary operator
418/// token. This returns:
419/// ~0 - Invalid token.
Chris Lattner3c57f7e2008-05-05 04:10:51 +0000420/// 14 -> 3 - various operators.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000421/// 0 - 'eod' or ')'
Chris Lattner22eb9722006-06-18 05:43:12 +0000422static unsigned getPrecedence(tok::TokenKind Kind) {
423 switch (Kind) {
424 default: return ~0U;
425 case tok::percent:
426 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000427 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000428 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000429 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000430 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000431 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000432 case tok::lessequal:
433 case tok::less:
434 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000435 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000436 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000437 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000438 case tok::amp: return 9;
439 case tok::caret: return 8;
440 case tok::pipe: return 7;
441 case tok::ampamp: return 6;
442 case tok::pipepipe: return 5;
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000443 case tok::question: return 4;
444 case tok::comma: return 3;
Chris Lattnerd89e4582008-05-04 18:36:18 +0000445 case tok::colon: return 2;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000446 case tok::r_paren: return 0;// Lowest priority, end of expr.
447 case tok::eod: return 0;// Lowest priority, end of directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000448 }
449}
450
451
452/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner3565c8e2008-05-05 06:45:50 +0000453/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Chris Lattner86054a92007-04-10 05:26:38 +0000454///
455/// If ValueLive is false, then this value is being evaluated in a context where
456/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner3565c8e2008-05-05 06:45:50 +0000457/// evaluation, such as division by zero warnings.
458static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000459 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +0000460 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000461 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
462 // If this token isn't valid, report the error.
463 if (PeekPrec == ~0U) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000464 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
465 << LHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000466 return true;
467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner22eb9722006-06-18 05:43:12 +0000469 while (1) {
470 // If this token has a lower precedence than we are allowed to parse, return
471 // it so that higher levels of the recursion can parse it.
472 if (PeekPrec < MinPrec)
473 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattner22eb9722006-06-18 05:43:12 +0000475 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner86054a92007-04-10 05:26:38 +0000477 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump11289f42009-09-09 15:08:12 +0000478 // dead. Note that this cannot just clobber ValueLive. Consider
Chris Lattner86054a92007-04-10 05:26:38 +0000479 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
480 // this example, the RHS of the && being dead does not make the rest of the
481 // expr dead.
482 bool RHSIsLive;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000483 if (Operator == tok::ampamp && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000484 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000485 else if (Operator == tok::pipepipe && LHS.Val != 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000486 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000487 else if (Operator == tok::question && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000488 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
489 else
490 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000491
Chris Lattner3565c8e2008-05-05 06:45:50 +0000492 // Consume the operator, remembering the operator's location for reporting.
493 SourceLocation OpLoc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000494 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000495
Chris Lattner3565c8e2008-05-05 06:45:50 +0000496 PPValue RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000497 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000498 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000499 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000500
501 // Remember the precedence of this operator and get the precedence of the
502 // operator immediately to the right of the RHS.
503 unsigned ThisPrec = PeekPrec;
504 PeekPrec = getPrecedence(PeekTok.getKind());
505
506 // If this token isn't valid, report the error.
507 if (PeekPrec == ~0U) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000508 PP.Diag(PeekTok.getLocation(), diag::err_pp_expr_bad_token_binop)
509 << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000510 return true;
511 }
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000513 // Decide whether to include the next binop in this subexpression. For
514 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattnerca2b3182008-05-05 20:09:27 +0000515 // handle y*z as a single subexpression. We do this because the precedence
516 // of * is higher than that of +. The only strange case we have to handle
517 // here is for the ?: operator, where the precedence is actually lower than
518 // the LHS of the '?'. The grammar rule is:
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000519 //
520 // conditional-expression ::=
521 // logical-OR-expression ? expression : conditional-expression
522 // where 'expression' is actually comma-expression.
523 unsigned RHSPrec;
524 if (Operator == tok::question)
525 // The RHS of "?" should be maximally consumed as an expression.
526 RHSPrec = getPrecedence(tok::comma);
527 else // All others should munch while higher precedence.
528 RHSPrec = ThisPrec+1;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000530 if (PeekPrec >= RHSPrec) {
531 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000532 return true;
533 PeekPrec = getPrecedence(PeekTok.getKind());
534 }
535 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000537 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump11289f42009-09-09 15:08:12 +0000538 // either operand is unsigned.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000539 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnerca671b02008-05-04 23:46:17 +0000540 switch (Operator) {
541 case tok::question: // No UAC for x and y in "x ? y : z".
542 case tok::lessless: // Shift amount doesn't UAC with shift value.
543 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
544 case tok::comma: // Comma operands are not subject to UACs.
545 case tok::pipepipe: // Logical || does not do UACs.
546 case tok::ampamp: // Logical && does not do UACs.
547 break; // No UAC
548 default:
Chris Lattner99ca0912007-04-11 04:14:45 +0000549 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
550 // If this just promoted something from signed to unsigned, and if the
551 // value was negative, warn about it.
552 if (ValueLive && Res.isUnsigned()) {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000553 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Chris Lattnere05c4df2008-11-18 21:48:13 +0000554 PP.Diag(OpLoc, diag::warn_pp_convert_lhs_to_positive)
555 << LHS.Val.toString(10, true) + " to " +
556 LHS.Val.toString(10, false)
557 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000558 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Chris Lattnere05c4df2008-11-18 21:48:13 +0000559 PP.Diag(OpLoc, diag::warn_pp_convert_rhs_to_positive)
560 << RHS.Val.toString(10, true) + " to " +
561 RHS.Val.toString(10, false)
562 << LHS.getRange() << RHS.getRange();
Chris Lattner99ca0912007-04-11 04:14:45 +0000563 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000564 LHS.Val.setIsUnsigned(Res.isUnsigned());
565 RHS.Val.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000566 }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner5a0f1642007-04-10 06:54:33 +0000568 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000569 switch (Operator) {
David Blaikie83d382b2011-09-23 05:06:16 +0000570 default: llvm_unreachable("Unknown operator token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000571 case tok::percent:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000572 if (RHS.Val != 0)
573 Res = LHS.Val % RHS.Val;
574 else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000575 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
576 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000577 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000578 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000579 break;
580 case tok::slash:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000581 if (RHS.Val != 0) {
Chris Lattner2edb9262010-10-13 23:46:56 +0000582 if (LHS.Val.isSigned())
583 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
584 else
585 Res = LHS.Val / RHS.Val;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000586 } else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000587 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
588 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000589 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000590 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000591 break;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000593 case tok::star:
Chris Lattner2edb9262010-10-13 23:46:56 +0000594 if (Res.isSigned())
595 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
596 else
597 Res = LHS.Val * RHS.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000598 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000599 case tok::lessless: {
600 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000601 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattner2edb9262010-10-13 23:46:56 +0000602 if (LHS.isUnsigned()) {
603 Overflow = ShAmt >= LHS.Val.getBitWidth();
604 if (Overflow)
605 ShAmt = LHS.Val.getBitWidth()-1;
606 Res = LHS.Val << ShAmt;
607 } else {
608 Res = llvm::APSInt(LHS.Val.sshl_ov(ShAmt, Overflow), false);
609 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000610 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000611 }
612 case tok::greatergreater: {
613 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000614 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattner5a0f1642007-04-10 06:54:33 +0000615 if (ShAmt >= LHS.getBitWidth())
616 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000617 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000618 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000619 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000620 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000621 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000622 Res = LHS.Val + RHS.Val;
623 else
624 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000625 break;
626 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000627 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000628 Res = LHS.Val - RHS.Val;
629 else
630 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000631 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000632 case tok::lessequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000633 Res = LHS.Val <= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000634 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000635 break;
636 case tok::less:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000637 Res = LHS.Val < RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000638 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000639 break;
640 case tok::greaterequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000641 Res = LHS.Val >= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000642 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000643 break;
644 case tok::greater:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000645 Res = LHS.Val > RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000646 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000647 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000648 case tok::exclaimequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000649 Res = LHS.Val != RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000650 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000651 break;
652 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000653 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000654 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000655 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000656 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000657 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000658 break;
659 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000660 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000661 break;
662 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000663 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000664 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000665 case tok::ampamp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000666 Res = (LHS.Val != 0 && RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000667 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000668 break;
669 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000670 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000671 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000672 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000673 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000674 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
675 // if not being evaluated.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000676 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000677 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
678 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000679 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000680 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000681 case tok::question: {
682 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000683 if (PeekTok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +0000684 PP.Diag(PeekTok.getLocation(), diag::err_expected)
685 << tok::colon << LHS.getRange() << RHS.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000686 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +0000687 return true;
688 }
689 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000690 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000691
692 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000693 bool AfterColonLive = ValueLive && LHS.Val == 0;
694 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000695 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000696 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
697 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000698
Chris Lattnerca2b3182008-05-05 20:09:27 +0000699 // Parse anything after the : with the same precedence as ?. We allow
700 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000701 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000702 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000703 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattner22eb9722006-06-18 05:43:12 +0000705 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000706 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
707 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000708
709 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
710 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000711 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000712
Chris Lattner22eb9722006-06-18 05:43:12 +0000713 // Figure out the precedence of the token after the : part.
714 PeekPrec = getPrecedence(PeekTok.getKind());
715 break;
716 }
717 case tok::colon:
718 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000719 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
720 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000721 return true;
722 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000723
724 // If this operator is live and overflowed, report the issue.
725 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000726 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
727 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattner9cc755d2007-04-10 07:07:11 +0000729 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000730 LHS.Val = Res;
731 LHS.setEnd(RHS.getRange().getEnd());
Chris Lattner22eb9722006-06-18 05:43:12 +0000732 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000733}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000734
735/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
736/// may occur after a #if or #elif directive. If the expression is equivalent
737/// to "!defined(X)" return X in IfNDefMacro.
738bool Preprocessor::
739EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikie1dc4a3d2013-03-18 23:22:28 +0000740 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattner4c53c402009-12-14 05:00:18 +0000741 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
742 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
743 // in which case a directive is undefined behavior. We want macros to be able
744 // to recursively expand in order to get more gcc-list behavior, so we force
745 // DisableMacroExpansion to false and restore it when we're done parsing the
746 // expression.
747 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
748 DisableMacroExpansion = false;
749
Chris Lattnere3519cc2006-07-04 18:11:39 +0000750 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000751 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000752 LexNonComment(Tok);
Douglas Gregor12785102010-08-24 20:21:13 +0000753
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000754 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000755 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattner3565c8e2008-05-05 06:45:50 +0000757 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000758 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000759 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000760 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000761 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000762 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000763
764 // Restore 'DisableMacroExpansion'.
765 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000766 return false;
767 }
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnere3519cc2006-07-04 18:11:39 +0000769 // If we are at the end of the expression after just parsing a value, there
770 // must be no (unparenthesized) binary operators involved, so we can exit
771 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000772 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000773 // If the expression we parsed was of the form !defined(macro), return the
774 // macro in IfNDefMacro.
775 if (DT.State == DefinedTracker::NotDefinedMacro)
776 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000777
Chris Lattner4c53c402009-12-14 05:00:18 +0000778 // Restore 'DisableMacroExpansion'.
779 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000780 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnere3519cc2006-07-04 18:11:39 +0000783 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
784 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000785 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
786 Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000787 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000788 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000789 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000790
791 // Restore 'DisableMacroExpansion'.
792 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000793 return false;
794 }
Mike Stump11289f42009-09-09 15:08:12 +0000795
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000796 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000797 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000798 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000799 Diag(Tok, diag::err_pp_expected_eol);
800 DiscardUntilEndOfDirective();
801 }
Mike Stump11289f42009-09-09 15:08:12 +0000802
Chris Lattner4c53c402009-12-14 05:00:18 +0000803 // Restore 'DisableMacroExpansion'.
804 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000805 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000806}