blob: d05ec672c8bf2d456414e81898b6dc676f21ea94 [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 Thompsoncda95fe2013-07-19 18:50:04 +000084 SourceLocation beginLoc(PeekTok.getLocation());
85 Result.setBegin(beginLoc);
John Thompsonb5353522009-10-30 13:49:06 +000086
87 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +000088 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +000089
90 // Two options, it can either be a pp-identifier or a (.
91 SourceLocation LParenLoc;
92 if (PeekTok.is(tok::l_paren)) {
93 // Found a paren, remember we saw it and skip it.
94 LParenLoc = PeekTok.getLocation();
Eli Friedmanb3bfd842011-08-03 00:04:13 +000095 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +000096 }
97
Douglas Gregor12785102010-08-24 20:21:13 +000098 if (PeekTok.is(tok::code_completion)) {
99 if (PP.getCodeCompletionHandler())
100 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000101 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000102 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000103 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000104
John Thompsonb5353522009-10-30 13:49:06 +0000105 // If we don't have a pp-identifier now, this is an error.
Serge Pavlovd024f522014-10-24 17:31:32 +0000106 if (PP.CheckMacroName(PeekTok, MU_Other))
John Thompsonb5353522009-10-30 13:49:06 +0000107 return true;
John Thompsonb5353522009-10-30 13:49:06 +0000108
109 // Otherwise, we got an identifier, is it defined to something?
Alp Tokerb05e0b52014-05-21 06:13:51 +0000110 IdentifierInfo *II = PeekTok.getIdentifierInfo();
Richard Smith20e883e2015-04-29 23:20:19 +0000111 Preprocessor::MacroDefinition Macro = PP.getMacroDefinition(II);
112 Result.Val = !!Macro;
John Thompsonb5353522009-10-30 13:49:06 +0000113 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
114
115 // If there is a macro, mark it used.
Richard Smith20e883e2015-04-29 23:20:19 +0000116 if (Result.Val != 0 && ValueLive)
117 PP.markMacroAsUsed(Macro.getMacroInfo());
John Thompsonb5353522009-10-30 13:49:06 +0000118
John Thompsoncda95fe2013-07-19 18:50:04 +0000119 // Save macro token for callback.
120 Token macroToken(PeekTok);
Douglas Gregor82e0d7282011-10-14 00:49:43 +0000121
John Thompsonb5353522009-10-30 13:49:06 +0000122 // If we are in parens, ensure we have a trailing ).
123 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000124 // Consume identifier.
125 Result.setEnd(PeekTok.getLocation());
126 PP.LexUnexpandedNonComment(PeekTok);
127
John Thompsonb5353522009-10-30 13:49:06 +0000128 if (PeekTok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +0000129 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
130 << "'defined'" << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +0000131 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsonb5353522009-10-30 13:49:06 +0000132 return true;
133 }
134 // Consume the ).
135 Result.setEnd(PeekTok.getLocation());
136 PP.LexNonComment(PeekTok);
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000137 } else {
138 // Consume identifier.
139 Result.setEnd(PeekTok.getLocation());
140 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000141 }
142
John Thompsoncda95fe2013-07-19 18:50:04 +0000143 // Invoke the 'defined' callback.
144 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
Richard Smith20e883e2015-04-29 23:20:19 +0000145 // FIXME: Tell callbacks about module macros.
146 MacroDirective *MD = Macro.getLocalDirective();
John Thompsoncda95fe2013-07-19 18:50:04 +0000147 Callbacks->Defined(macroToken, MD,
148 SourceRange(beginLoc, PeekTok.getLocation()));
149 }
150
John Thompsonb5353522009-10-30 13:49:06 +0000151 // Success, remember that we saw defined(X).
152 DT.State = DefinedTracker::DefinedMacro;
153 DT.TheMacro = II;
154 return false;
155}
156
Chris Lattner22eb9722006-06-18 05:43:12 +0000157/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
158/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000159/// parsing. This function also returns information about the form of the
160/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000161///
162/// If ValueLive is false, then this value is being evaluated in a context where
163/// the result is not used. As such, avoid diagnostics that relate to
164/// evaluation.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000165static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
166 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000167 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000168
Douglas Gregorec00a262010-08-24 22:20:20 +0000169 if (PeekTok.is(tok::code_completion)) {
170 if (PP.getCodeCompletionHandler())
171 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000172 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000173 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000174 }
175
Chris Lattner22eb9722006-06-18 05:43:12 +0000176 // If this token's spelling is a pp-identifier, check to see if it is
177 // 'defined' or if it is a macro. Note that we check here because many
178 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000179 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner676268e2009-12-14 04:26:45 +0000180 // Handle "defined X" and "defined(X)".
181 if (II->isStr("defined"))
John Thompsonb5353522009-10-30 13:49:06 +0000182 return(EvaluateDefined(Result, PeekTok, DT, ValueLive, PP));
Chris Lattner676268e2009-12-14 04:26:45 +0000183
184 // If this identifier isn't 'defined' or one of the special
185 // preprocessor keywords and it wasn't macro expanded, it turns
186 // into a simple 0, unless it is the C++ keyword "true", in which case it
187 // turns into "1".
Eli Friedman847f3ca2012-09-20 02:38:38 +0000188 if (ValueLive &&
189 II->getTokenID() != tok::kw_true &&
190 II->getTokenID() != tok::kw_false)
Chris Lattner676268e2009-12-14 04:26:45 +0000191 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
192 Result.Val = II->getTokenID() == tok::kw_true;
193 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
194 Result.setRange(PeekTok.getLocation());
195 PP.LexNonComment(PeekTok);
196 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner22eb9722006-06-18 05:43:12 +0000199 switch (PeekTok.getKind()) {
200 default: // Non-value token.
Chris Lattnerf8f94542008-04-13 20:38:43 +0000201 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000202 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000203 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000204 case tok::r_paren:
205 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000206 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000207 return true;
208 case tok::numeric_constant: {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000209 SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000210 bool NumberInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000211 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000212 &NumberInvalid);
213 if (NumberInvalid)
214 return true; // a diagnostic was already reported
215
Dmitri Gribenko7ba91722012-09-24 09:53:54 +0000216 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000217 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000218 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattnered045422007-08-26 03:29:23 +0000220 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000221 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000222 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000223 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000224 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000225
Richard Smith39570d002012-03-08 08:45:32 +0000226 // Complain about, and drop, any ud-suffix.
227 if (Literal.hasUDSuffix())
228 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
229
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000230 // 'long long' is a C99 or C++11 feature.
231 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
232 if (PP.getLangOpts().CPlusPlus)
233 PP.Diag(PeekTok,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000234 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000235 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
236 else
237 PP.Diag(PeekTok, diag::ext_c99_longlong);
238 }
Neil Boothac582c52007-08-29 22:00:19 +0000239
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000240 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000241 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000242 // Overflow parsing integer literal.
Aaron Ballman446867e2014-07-22 14:08:09 +0000243 if (ValueLive)
Aaron Ballman31f42312014-07-24 14:51:23 +0000244 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
245 << /* Unsigned */ 1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000246 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000247 } else {
248 // Set the signedness of the result to match whether there was a U suffix
249 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000250 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000252 // Detect overflow based on whether the value is signed. If signed
253 // and if the value is too large, emit a warning "integer constant is so
254 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
255 // is 64-bits.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000256 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Richard Smith7e34fbd2014-03-14 21:21:24 +0000257 // Octal, hexadecimal, and binary literals are implicitly unsigned if
258 // the value does not fit into a signed integer type.
Eli Friedman088d39a2013-07-23 00:25:18 +0000259 if (ValueLive && Literal.getRadix() == 10)
Aaron Ballman31f42312014-07-24 14:51:23 +0000260 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000261 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000262 }
263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000265 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000266 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000267 PP.LexNonComment(PeekTok);
268 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000269 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000270 case tok::char_constant: // 'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000271 case tok::wide_char_constant: // L'x'
Richard Smith3e3a7052014-11-08 06:08:42 +0000272 case tok::utf8_char_constant: // u8'x'
Douglas Gregorfb65e592011-07-27 05:40:30 +0000273 case tok::utf16_char_constant: // u'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000274 case tok::utf32_char_constant: { // U'x'
Richard Smithd67aea22012-03-06 03:21:47 +0000275 // Complain about, and drop, any ud-suffix.
276 if (PeekTok.hasUDSuffix())
Richard Smith39570d002012-03-08 08:45:32 +0000277 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smithd67aea22012-03-06 03:21:47 +0000278
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000279 SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000280 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000281 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000282 if (CharInvalid)
283 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000284
285 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000286 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000287 if (Literal.hadError())
288 return true; // A diagnostic was already emitted.
289
290 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000291 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000292 unsigned NumBits;
293 if (Literal.isMultiChar())
294 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000295 else if (Literal.isWide())
296 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000297 else if (Literal.isUTF16())
298 NumBits = TI.getChar16Width();
299 else if (Literal.isUTF32())
300 NumBits = TI.getChar32Width();
Eli Friedmand8cec572009-06-01 05:25:02 +0000301 else
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000302 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000303
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000304 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000305 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000306 // Set the value.
307 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000308 // Set the signedness. UTF-16 and UTF-32 are always unsigned
Michael Wong867eeb52015-02-24 13:34:20 +0000309 if (Literal.isWide())
310 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
311 else 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.
David Majnemer3e8b6ac2014-10-13 22:18:22 +0000601 if (LHS.isUnsigned())
602 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
603 else
604 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000605 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000606 }
607 case tok::greatergreater: {
608 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000609 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Chris Lattner5a0f1642007-04-10 06:54:33 +0000610 if (ShAmt >= LHS.getBitWidth())
611 Overflow = true, ShAmt = LHS.getBitWidth()-1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000612 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000613 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000614 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000615 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000616 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000617 Res = LHS.Val + RHS.Val;
618 else
619 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000620 break;
621 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000622 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000623 Res = LHS.Val - RHS.Val;
624 else
625 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000626 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000627 case tok::lessequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000628 Res = LHS.Val <= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000629 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000630 break;
631 case tok::less:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000632 Res = LHS.Val < RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000633 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000634 break;
635 case tok::greaterequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000636 Res = LHS.Val >= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000637 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000638 break;
639 case tok::greater:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000640 Res = LHS.Val > RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000641 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000642 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000643 case tok::exclaimequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000644 Res = LHS.Val != RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000645 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000646 break;
647 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000648 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000649 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000650 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000651 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000652 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000653 break;
654 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000655 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000656 break;
657 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000658 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000659 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000660 case tok::ampamp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000661 Res = (LHS.Val != 0 && RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000662 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000663 break;
664 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000665 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000666 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000667 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000668 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000669 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
670 // if not being evaluated.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000671 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000672 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
673 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000674 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000675 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000676 case tok::question: {
677 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000678 if (PeekTok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +0000679 PP.Diag(PeekTok.getLocation(), diag::err_expected)
680 << tok::colon << LHS.getRange() << RHS.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000681 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +0000682 return true;
683 }
684 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000685 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000686
687 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000688 bool AfterColonLive = ValueLive && LHS.Val == 0;
689 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000690 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000691 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
692 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000693
Chris Lattnerca2b3182008-05-05 20:09:27 +0000694 // Parse anything after the : with the same precedence as ?. We allow
695 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000696 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000697 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000698 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000699
Chris Lattner22eb9722006-06-18 05:43:12 +0000700 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000701 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
702 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000703
704 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
705 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000706 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000707
Chris Lattner22eb9722006-06-18 05:43:12 +0000708 // Figure out the precedence of the token after the : part.
709 PeekPrec = getPrecedence(PeekTok.getKind());
710 break;
711 }
712 case tok::colon:
713 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000714 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
715 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000716 return true;
717 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000718
719 // If this operator is live and overflowed, report the issue.
720 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000721 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
722 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner9cc755d2007-04-10 07:07:11 +0000724 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000725 LHS.Val = Res;
726 LHS.setEnd(RHS.getRange().getEnd());
Chris Lattner22eb9722006-06-18 05:43:12 +0000727 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000728}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000729
730/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
731/// may occur after a #if or #elif directive. If the expression is equivalent
732/// to "!defined(X)" return X in IfNDefMacro.
Yaron Keren203439f2015-05-02 19:29:29 +0000733bool Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikie1dc4a3d2013-03-18 23:22:28 +0000734 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattner4c53c402009-12-14 05:00:18 +0000735 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
736 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
737 // in which case a directive is undefined behavior. We want macros to be able
738 // to recursively expand in order to get more gcc-list behavior, so we force
739 // DisableMacroExpansion to false and restore it when we're done parsing the
740 // expression.
741 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
742 DisableMacroExpansion = false;
743
Chris Lattnere3519cc2006-07-04 18:11:39 +0000744 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000745 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000746 LexNonComment(Tok);
Douglas Gregor12785102010-08-24 20:21:13 +0000747
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000748 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000749 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattner3565c8e2008-05-05 06:45:50 +0000751 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000752 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000753 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000754 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000755 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000756 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000757
758 // Restore 'DisableMacroExpansion'.
759 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000760 return false;
761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattnere3519cc2006-07-04 18:11:39 +0000763 // If we are at the end of the expression after just parsing a value, there
764 // must be no (unparenthesized) binary operators involved, so we can exit
765 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000766 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000767 // If the expression we parsed was of the form !defined(macro), return the
768 // macro in IfNDefMacro.
769 if (DT.State == DefinedTracker::NotDefinedMacro)
770 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner4c53c402009-12-14 05:00:18 +0000772 // Restore 'DisableMacroExpansion'.
773 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000774 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattnere3519cc2006-07-04 18:11:39 +0000777 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
778 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000779 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
780 Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000781 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000782 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000783 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000784
785 // Restore 'DisableMacroExpansion'.
786 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000787 return false;
788 }
Mike Stump11289f42009-09-09 15:08:12 +0000789
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000790 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000791 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000792 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000793 Diag(Tok, diag::err_pp_expected_eol);
794 DiscardUntilEndOfDirective();
795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattner4c53c402009-12-14 05:00:18 +0000797 // Restore 'DisableMacroExpansion'.
798 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000799 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000800}