blob: 862a4713e4bca1c9af238c6fc98f4a92963a4e54 [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"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000020#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner81278c62006-10-14 19:03:49 +000023#include "clang/Basic/TargetInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000024#include "clang/Basic/TokenKinds.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattner60f36222009-01-29 05:15:15 +000026#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "clang/Lex/LiteralSupport.h"
28#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000029#include "clang/Lex/PPCallbacks.h"
30#include "clang/Lex/Token.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000031#include "llvm/ADT/APSInt.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000032#include "llvm/ADT/SmallString.h"
33#include "llvm/ADT/StringRef.h"
David Blaikie76bd3c82011-09-23 05:35:21 +000034#include "llvm/Support/ErrorHandling.h"
David Blaikie1dc4a3d2013-03-18 23:22:28 +000035#include "llvm/Support/SaveAndRestore.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000036#include <cassert>
37
Chris Lattner22eb9722006-06-18 05:43:12 +000038using namespace clang;
39
Dan Gohman28ade552010-07-26 21:25:24 +000040namespace {
41
Chris Lattner3565c8e2008-05-05 06:45:50 +000042/// PPValue - Represents the value of a subexpression of a preprocessor
43/// conditional and the source range covered by it.
44class PPValue {
45 SourceRange Range;
Richard Smith4d247e72016-04-16 00:07:09 +000046 IdentifierInfo *II;
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000047
Chris Lattner3565c8e2008-05-05 06:45:50 +000048public:
49 llvm::APSInt Val;
Mike Stump11289f42009-09-09 15:08:12 +000050
Chris Lattner3565c8e2008-05-05 06:45:50 +000051 // Default ctor - Construct an 'invalid' PPValue.
52 PPValue(unsigned BitWidth) : Val(BitWidth) {}
Mike Stump11289f42009-09-09 15:08:12 +000053
Richard Smith4d247e72016-04-16 00:07:09 +000054 // If this value was produced by directly evaluating an identifier, produce
55 // that identifier.
56 IdentifierInfo *getIdentifier() const { return II; }
57 void setIdentifier(IdentifierInfo *II) { this->II = II; }
58
Chris Lattner3565c8e2008-05-05 06:45:50 +000059 unsigned getBitWidth() const { return Val.getBitWidth(); }
60 bool isUnsigned() const { return Val.isUnsigned(); }
Mike Stump11289f42009-09-09 15:08:12 +000061
Craig Toppere335f252015-10-04 04:53:55 +000062 SourceRange getRange() const { return Range; }
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattner3565c8e2008-05-05 06:45:50 +000064 void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
65 void setRange(SourceLocation B, SourceLocation E) {
Mike Stump11289f42009-09-09 15:08:12 +000066 Range.setBegin(B); Range.setEnd(E);
Chris Lattner3565c8e2008-05-05 06:45:50 +000067 }
68 void setBegin(SourceLocation L) { Range.setBegin(L); }
69 void setEnd(SourceLocation L) { Range.setEnd(L); }
70};
71
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000072} // end anonymous namespace
Dan Gohman28ade552010-07-26 21:25:24 +000073
Chris Lattner3565c8e2008-05-05 06:45:50 +000074static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +000075 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +000076 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000077
Chris Lattnerb9d90f72006-07-04 18:32:03 +000078/// DefinedTracker - This struct is used while parsing expressions to keep track
79/// of whether !defined(X) has been seen.
80///
81/// With this simple scheme, we handle the basic forms:
82/// !defined(X) and !defined X
83/// but we also trivially handle (silly) stuff like:
84/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
85struct DefinedTracker {
86 /// Each time a Value is evaluated, it returns information about whether the
87 /// parsed value is of the form defined(X), !defined(X) or is something else.
88 enum TrackerState {
89 DefinedMacro, // defined(X)
90 NotDefinedMacro, // !defined(X)
91 Unknown // Something else.
92 } State;
93 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
94 /// indicates the macro that was checked.
95 IdentifierInfo *TheMacro;
96};
97
John Thompsonb5353522009-10-30 13:49:06 +000098/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattner4c53c402009-12-14 05:00:18 +000099static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
100 bool ValueLive, Preprocessor &PP) {
John Thompsoncda95fe2013-07-19 18:50:04 +0000101 SourceLocation beginLoc(PeekTok.getLocation());
102 Result.setBegin(beginLoc);
John Thompsonb5353522009-10-30 13:49:06 +0000103
104 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000105 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000106
107 // Two options, it can either be a pp-identifier or a (.
108 SourceLocation LParenLoc;
109 if (PeekTok.is(tok::l_paren)) {
110 // Found a paren, remember we saw it and skip it.
111 LParenLoc = PeekTok.getLocation();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000112 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000113 }
114
Douglas Gregor12785102010-08-24 20:21:13 +0000115 if (PeekTok.is(tok::code_completion)) {
116 if (PP.getCodeCompletionHandler())
117 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000118 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000119 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000120 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000121
John Thompsonb5353522009-10-30 13:49:06 +0000122 // If we don't have a pp-identifier now, this is an error.
Serge Pavlovd024f522014-10-24 17:31:32 +0000123 if (PP.CheckMacroName(PeekTok, MU_Other))
John Thompsonb5353522009-10-30 13:49:06 +0000124 return true;
John Thompsonb5353522009-10-30 13:49:06 +0000125
126 // Otherwise, we got an identifier, is it defined to something?
Alp Tokerb05e0b52014-05-21 06:13:51 +0000127 IdentifierInfo *II = PeekTok.getIdentifierInfo();
Richard Smith66a81862015-05-04 02:25:31 +0000128 MacroDefinition Macro = PP.getMacroDefinition(II);
Richard Smith20e883e2015-04-29 23:20:19 +0000129 Result.Val = !!Macro;
Richard Smith66a81862015-05-04 02:25:31 +0000130 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
John Thompsonb5353522009-10-30 13:49:06 +0000131
132 // If there is a macro, mark it used.
Richard Smith20e883e2015-04-29 23:20:19 +0000133 if (Result.Val != 0 && ValueLive)
134 PP.markMacroAsUsed(Macro.getMacroInfo());
John Thompsonb5353522009-10-30 13:49:06 +0000135
John Thompsoncda95fe2013-07-19 18:50:04 +0000136 // Save macro token for callback.
137 Token macroToken(PeekTok);
Douglas Gregor82e0d7282011-10-14 00:49:43 +0000138
John Thompsonb5353522009-10-30 13:49:06 +0000139 // If we are in parens, ensure we have a trailing ).
140 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000141 // Consume identifier.
142 Result.setEnd(PeekTok.getLocation());
143 PP.LexUnexpandedNonComment(PeekTok);
144
John Thompsonb5353522009-10-30 13:49:06 +0000145 if (PeekTok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +0000146 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
147 << "'defined'" << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +0000148 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsonb5353522009-10-30 13:49:06 +0000149 return true;
150 }
151 // Consume the ).
152 Result.setEnd(PeekTok.getLocation());
153 PP.LexNonComment(PeekTok);
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000154 } else {
155 // Consume identifier.
156 Result.setEnd(PeekTok.getLocation());
157 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000158 }
159
Nico Weberb2348f42016-01-19 15:15:31 +0000160 // [cpp.cond]p4:
161 // Prior to evaluation, macro invocations in the list of preprocessing
162 // tokens that will become the controlling constant expression are replaced
163 // (except for those macro names modified by the 'defined' unary operator),
164 // just as in normal text. If the token 'defined' is generated as a result
165 // of this replacement process or use of the 'defined' unary operator does
166 // not match one of the two specified forms prior to macro replacement, the
167 // behavior is undefined.
168 // This isn't an idle threat, consider this program:
169 // #define FOO
170 // #define BAR defined(FOO)
171 // #if BAR
172 // ...
173 // #else
174 // ...
175 // #endif
176 // clang and gcc will pick the #if branch while Visual Studio will take the
177 // #else branch. Emit a warning about this undefined behavior.
178 if (beginLoc.isMacroID()) {
179 bool IsFunctionTypeMacro =
180 PP.getSourceManager()
181 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
182 .getExpansion()
183 .isFunctionMacroExpansion();
184 // For object-type macros, it's easy to replace
185 // #define FOO defined(BAR)
186 // with
187 // #if defined(BAR)
188 // #define FOO 1
189 // #else
190 // #define FOO 0
191 // #endif
192 // and doing so makes sense since compilers handle this differently in
193 // practice (see example further up). But for function-type macros,
194 // there is no good way to write
195 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
196 // in a different way, and compilers seem to agree on how to behave here.
197 // So warn by default on object-type macros, but only warn in -pedantic
198 // mode on function-type macros.
199 if (IsFunctionTypeMacro)
200 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
201 else
202 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
203 }
204
John Thompsoncda95fe2013-07-19 18:50:04 +0000205 // Invoke the 'defined' callback.
206 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000207 Callbacks->Defined(macroToken, Macro,
John Thompsoncda95fe2013-07-19 18:50:04 +0000208 SourceRange(beginLoc, PeekTok.getLocation()));
209 }
210
John Thompsonb5353522009-10-30 13:49:06 +0000211 // Success, remember that we saw defined(X).
212 DT.State = DefinedTracker::DefinedMacro;
213 DT.TheMacro = II;
214 return false;
215}
216
Chris Lattner22eb9722006-06-18 05:43:12 +0000217/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
218/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000219/// parsing. This function also returns information about the form of the
220/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000221///
222/// If ValueLive is false, then this value is being evaluated in a context where
223/// the result is not used. As such, avoid diagnostics that relate to
224/// evaluation.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000225static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
226 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000227 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000228
Richard Smith4d247e72016-04-16 00:07:09 +0000229 Result.setIdentifier(nullptr);
230
Douglas Gregorec00a262010-08-24 22:20:20 +0000231 if (PeekTok.is(tok::code_completion)) {
232 if (PP.getCodeCompletionHandler())
233 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000234 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000235 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000236 }
237
Chris Lattner22eb9722006-06-18 05:43:12 +0000238 // If this token's spelling is a pp-identifier, check to see if it is
239 // 'defined' or if it is a macro. Note that we check here because many
240 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000241 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner676268e2009-12-14 04:26:45 +0000242 // Handle "defined X" and "defined(X)".
243 if (II->isStr("defined"))
Nico Weberb2348f42016-01-19 15:15:31 +0000244 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
245
Chris Lattner676268e2009-12-14 04:26:45 +0000246 // If this identifier isn't 'defined' or one of the special
247 // preprocessor keywords and it wasn't macro expanded, it turns
248 // into a simple 0, unless it is the C++ keyword "true", in which case it
249 // turns into "1".
Eli Friedman847f3ca2012-09-20 02:38:38 +0000250 if (ValueLive &&
251 II->getTokenID() != tok::kw_true &&
252 II->getTokenID() != tok::kw_false)
Chris Lattner676268e2009-12-14 04:26:45 +0000253 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
254 Result.Val = II->getTokenID() == tok::kw_true;
255 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
Richard Smith4d247e72016-04-16 00:07:09 +0000256 Result.setIdentifier(II);
Chris Lattner676268e2009-12-14 04:26:45 +0000257 Result.setRange(PeekTok.getLocation());
258 PP.LexNonComment(PeekTok);
259 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000260 }
Mike Stump11289f42009-09-09 15:08:12 +0000261
Chris Lattner22eb9722006-06-18 05:43:12 +0000262 switch (PeekTok.getKind()) {
263 default: // Non-value token.
Chris Lattnerf8f94542008-04-13 20:38:43 +0000264 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000265 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000266 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000267 case tok::r_paren:
268 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000269 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000270 return true;
271 case tok::numeric_constant: {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000272 SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000273 bool NumberInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000274 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000275 &NumberInvalid);
276 if (NumberInvalid)
277 return true; // a diagnostic was already reported
278
Dmitri Gribenko7ba91722012-09-24 09:53:54 +0000279 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000280 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000281 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattnered045422007-08-26 03:29:23 +0000283 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000284 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000285 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000286 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000287 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000288
Richard Smith39570d002012-03-08 08:45:32 +0000289 // Complain about, and drop, any ud-suffix.
290 if (Literal.hasUDSuffix())
291 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
292
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000293 // 'long long' is a C99 or C++11 feature.
294 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
295 if (PP.getLangOpts().CPlusPlus)
296 PP.Diag(PeekTok,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000297 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000298 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
299 else
300 PP.Diag(PeekTok, diag::ext_c99_longlong);
301 }
Neil Boothac582c52007-08-29 22:00:19 +0000302
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000303 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000304 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000305 // Overflow parsing integer literal.
Aaron Ballman446867e2014-07-22 14:08:09 +0000306 if (ValueLive)
Aaron Ballman31f42312014-07-24 14:51:23 +0000307 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
308 << /* Unsigned */ 1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000309 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000310 } else {
311 // Set the signedness of the result to match whether there was a U suffix
312 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000313 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000315 // Detect overflow based on whether the value is signed. If signed
316 // and if the value is too large, emit a warning "integer constant is so
317 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
318 // is 64-bits.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000319 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Richard Smith7e34fbd2014-03-14 21:21:24 +0000320 // Octal, hexadecimal, and binary literals are implicitly unsigned if
321 // the value does not fit into a signed integer type.
Eli Friedman088d39a2013-07-23 00:25:18 +0000322 if (ValueLive && Literal.getRadix() == 10)
Aaron Ballman31f42312014-07-24 14:51:23 +0000323 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000324 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000325 }
326 }
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000328 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000329 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000330 PP.LexNonComment(PeekTok);
331 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000332 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000333 case tok::char_constant: // 'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000334 case tok::wide_char_constant: // L'x'
Richard Smith3e3a7052014-11-08 06:08:42 +0000335 case tok::utf8_char_constant: // u8'x'
Douglas Gregorfb65e592011-07-27 05:40:30 +0000336 case tok::utf16_char_constant: // u'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000337 case tok::utf32_char_constant: { // U'x'
Richard Smithd67aea22012-03-06 03:21:47 +0000338 // Complain about, and drop, any ud-suffix.
339 if (PeekTok.hasUDSuffix())
Richard Smith39570d002012-03-08 08:45:32 +0000340 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smithd67aea22012-03-06 03:21:47 +0000341
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000342 SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000343 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000344 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000345 if (CharInvalid)
346 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000347
348 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000349 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000350 if (Literal.hadError())
351 return true; // A diagnostic was already emitted.
352
353 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000354 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000355 unsigned NumBits;
356 if (Literal.isMultiChar())
357 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000358 else if (Literal.isWide())
359 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000360 else if (Literal.isUTF16())
361 NumBits = TI.getChar16Width();
362 else if (Literal.isUTF32())
363 NumBits = TI.getChar32Width();
Eli Friedmand8cec572009-06-01 05:25:02 +0000364 else
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000365 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000366
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000367 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000368 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000369 // Set the value.
370 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000371 // Set the signedness. UTF-16 and UTF-32 are always unsigned
Michael Wong867eeb52015-02-24 13:34:20 +0000372 if (Literal.isWide())
373 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
374 else if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikiebbafb8a2012-03-11 07:00:24 +0000375 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattner3565c8e2008-05-05 06:45:50 +0000377 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
378 Result.Val = Val.extend(Result.Val.getBitWidth());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000379 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000380 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000381 "intmax_t smaller than char/wchar_t?");
Chris Lattner3565c8e2008-05-05 06:45:50 +0000382 Result.Val = Val;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000383 }
384
385 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000386 Result.setRange(PeekTok.getLocation());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000387 PP.LexNonComment(PeekTok);
388 return false;
389 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000390 case tok::l_paren: {
391 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000392 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000393 // Parse the value and if there are any binary operators involved, parse
394 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000395 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000396
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000397 // If this is a silly value like (X), which doesn't need parens, check for
398 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000399 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000400 // Just use DT unmodified as our result.
401 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000402 // Otherwise, we have something like (x+y), and we consumed '(x'.
Chris Lattner86054a92007-04-10 05:26:38 +0000403 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive, PP))
404 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000406 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000407 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
408 << Result.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000409 PP.Diag(Start, diag::note_matching) << tok::l_paren;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000410 return true;
411 }
412 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000413 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000414 Result.setRange(Start, PeekTok.getLocation());
Richard Smith4d247e72016-04-16 00:07:09 +0000415 Result.setIdentifier(nullptr);
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000416 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000417 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000418 }
419 case tok::plus: {
420 SourceLocation Start = PeekTok.getLocation();
Chris Lattner22eb9722006-06-18 05:43:12 +0000421 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000422 PP.LexNonComment(PeekTok);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000423 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
424 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000425 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000426 return false;
427 }
Chris Lattner7e61ac52007-04-11 03:42:36 +0000428 case tok::minus: {
429 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000430 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000431 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000432 Result.setBegin(Loc);
Richard Smith4d247e72016-04-16 00:07:09 +0000433 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000435 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000436 Result.Val = -Result.Val;
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner1cb0e612008-07-03 03:47:30 +0000438 // -MININT is the only thing that overflows. Unsigned never overflows.
439 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner7e61ac52007-04-11 03:42:36 +0000441 // If this operator is live and overflowed, report the issue.
442 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000443 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000445 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000446 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattner3565c8e2008-05-05 06:45:50 +0000449 case tok::tilde: {
450 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000451 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000452 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000453 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000454 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000455
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000456 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000457 Result.Val = ~Result.Val;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000458 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000459 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner3565c8e2008-05-05 06:45:50 +0000462 case tok::exclaim: {
463 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000464 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000465 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000466 Result.setBegin(Start);
467 Result.Val = !Result.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000468 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000469 Result.Val.setIsUnsigned(false);
Richard Smith4d247e72016-04-16 00:07:09 +0000470 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000471
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000472 if (DT.State == DefinedTracker::DefinedMacro)
473 DT.State = DefinedTracker::NotDefinedMacro;
474 else if (DT.State == DefinedTracker::NotDefinedMacro)
475 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000476 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner22eb9722006-06-18 05:43:12 +0000479 // FIXME: Handle #assert
480 }
481}
482
Chris Lattner22eb9722006-06-18 05:43:12 +0000483/// getPrecedence - Return the precedence of the specified binary operator
484/// token. This returns:
485/// ~0 - Invalid token.
Chris Lattner3c57f7e2008-05-05 04:10:51 +0000486/// 14 -> 3 - various operators.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000487/// 0 - 'eod' or ')'
Chris Lattner22eb9722006-06-18 05:43:12 +0000488static unsigned getPrecedence(tok::TokenKind Kind) {
489 switch (Kind) {
490 default: return ~0U;
491 case tok::percent:
492 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000493 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000494 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000495 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000496 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000497 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000498 case tok::lessequal:
499 case tok::less:
500 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000501 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000502 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000503 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000504 case tok::amp: return 9;
505 case tok::caret: return 8;
506 case tok::pipe: return 7;
507 case tok::ampamp: return 6;
508 case tok::pipepipe: return 5;
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000509 case tok::question: return 4;
510 case tok::comma: return 3;
Chris Lattnerd89e4582008-05-04 18:36:18 +0000511 case tok::colon: return 2;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000512 case tok::r_paren: return 0;// Lowest priority, end of expr.
513 case tok::eod: return 0;// Lowest priority, end of directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000514 }
515}
516
Richard Smith4d247e72016-04-16 00:07:09 +0000517static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
518 Token &Tok) {
519 if (Tok.is(tok::l_paren) && LHS.getIdentifier())
520 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
521 << LHS.getIdentifier();
522 else
523 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
524 << LHS.getRange();
525}
Chris Lattner22eb9722006-06-18 05:43:12 +0000526
527/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner3565c8e2008-05-05 06:45:50 +0000528/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Chris Lattner86054a92007-04-10 05:26:38 +0000529///
530/// If ValueLive is false, then this value is being evaluated in a context where
531/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner3565c8e2008-05-05 06:45:50 +0000532/// evaluation, such as division by zero warnings.
533static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000534 Token &PeekTok, bool ValueLive,
Chris Lattner86054a92007-04-10 05:26:38 +0000535 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000536 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
537 // If this token isn't valid, report the error.
538 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000539 diagnoseUnexpectedOperator(PP, LHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000540 return true;
541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000543 while (true) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000544 // If this token has a lower precedence than we are allowed to parse, return
545 // it so that higher levels of the recursion can parse it.
546 if (PeekPrec < MinPrec)
547 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattner22eb9722006-06-18 05:43:12 +0000549 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump11289f42009-09-09 15:08:12 +0000550
Chris Lattner86054a92007-04-10 05:26:38 +0000551 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump11289f42009-09-09 15:08:12 +0000552 // dead. Note that this cannot just clobber ValueLive. Consider
Chris Lattner86054a92007-04-10 05:26:38 +0000553 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
554 // this example, the RHS of the && being dead does not make the rest of the
555 // expr dead.
556 bool RHSIsLive;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000557 if (Operator == tok::ampamp && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000558 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000559 else if (Operator == tok::pipepipe && LHS.Val != 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000560 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000561 else if (Operator == tok::question && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000562 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
563 else
564 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000565
Chris Lattner3565c8e2008-05-05 06:45:50 +0000566 // Consume the operator, remembering the operator's location for reporting.
567 SourceLocation OpLoc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000568 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000569
Chris Lattner3565c8e2008-05-05 06:45:50 +0000570 PPValue RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000571 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000572 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000573 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000574
575 // Remember the precedence of this operator and get the precedence of the
576 // operator immediately to the right of the RHS.
577 unsigned ThisPrec = PeekPrec;
578 PeekPrec = getPrecedence(PeekTok.getKind());
579
580 // If this token isn't valid, report the error.
581 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000582 diagnoseUnexpectedOperator(PP, RHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000583 return true;
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000586 // Decide whether to include the next binop in this subexpression. For
587 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattnerca2b3182008-05-05 20:09:27 +0000588 // handle y*z as a single subexpression. We do this because the precedence
589 // of * is higher than that of +. The only strange case we have to handle
590 // here is for the ?: operator, where the precedence is actually lower than
591 // the LHS of the '?'. The grammar rule is:
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000592 //
593 // conditional-expression ::=
594 // logical-OR-expression ? expression : conditional-expression
595 // where 'expression' is actually comma-expression.
596 unsigned RHSPrec;
597 if (Operator == tok::question)
598 // The RHS of "?" should be maximally consumed as an expression.
599 RHSPrec = getPrecedence(tok::comma);
600 else // All others should munch while higher precedence.
601 RHSPrec = ThisPrec+1;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000603 if (PeekPrec >= RHSPrec) {
604 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000605 return true;
606 PeekPrec = getPrecedence(PeekTok.getKind());
607 }
608 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000610 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump11289f42009-09-09 15:08:12 +0000611 // either operand is unsigned.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000612 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnerca671b02008-05-04 23:46:17 +0000613 switch (Operator) {
614 case tok::question: // No UAC for x and y in "x ? y : z".
615 case tok::lessless: // Shift amount doesn't UAC with shift value.
616 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
617 case tok::comma: // Comma operands are not subject to UACs.
618 case tok::pipepipe: // Logical || does not do UACs.
619 case tok::ampamp: // Logical && does not do UACs.
620 break; // No UAC
621 default:
Chris Lattner99ca0912007-04-11 04:14:45 +0000622 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
623 // If this just promoted something from signed to unsigned, and if the
624 // value was negative, warn about it.
625 if (ValueLive && Res.isUnsigned()) {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000626 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000627 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
Chris Lattnere05c4df2008-11-18 21:48:13 +0000628 << LHS.Val.toString(10, true) + " to " +
629 LHS.Val.toString(10, false)
630 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000631 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000632 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
Chris Lattnere05c4df2008-11-18 21:48:13 +0000633 << RHS.Val.toString(10, true) + " to " +
634 RHS.Val.toString(10, false)
635 << LHS.getRange() << RHS.getRange();
Chris Lattner99ca0912007-04-11 04:14:45 +0000636 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000637 LHS.Val.setIsUnsigned(Res.isUnsigned());
638 RHS.Val.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner5a0f1642007-04-10 06:54:33 +0000641 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000642 switch (Operator) {
David Blaikie83d382b2011-09-23 05:06:16 +0000643 default: llvm_unreachable("Unknown operator token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000644 case tok::percent:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000645 if (RHS.Val != 0)
646 Res = LHS.Val % RHS.Val;
647 else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000648 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
649 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000650 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000651 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000652 break;
653 case tok::slash:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000654 if (RHS.Val != 0) {
Chris Lattner2edb9262010-10-13 23:46:56 +0000655 if (LHS.Val.isSigned())
656 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
657 else
658 Res = LHS.Val / RHS.Val;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000659 } else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000660 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
661 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000662 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000663 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000664 break;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000666 case tok::star:
Chris Lattner2edb9262010-10-13 23:46:56 +0000667 if (Res.isSigned())
668 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
669 else
670 Res = LHS.Val * RHS.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000671 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000672 case tok::lessless: {
673 // Determine whether overflow is about to happen.
David Majnemer3e8b6ac2014-10-13 22:18:22 +0000674 if (LHS.isUnsigned())
675 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
676 else
677 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000678 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000679 }
680 case tok::greatergreater: {
681 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000682 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Richard Trieucc3949d2016-02-18 22:34:54 +0000683 if (ShAmt >= LHS.getBitWidth()) {
684 Overflow = true;
685 ShAmt = LHS.getBitWidth()-1;
686 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000687 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000688 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000689 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000690 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000691 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000692 Res = LHS.Val + RHS.Val;
693 else
694 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000695 break;
696 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000697 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000698 Res = LHS.Val - RHS.Val;
699 else
700 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000701 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000702 case tok::lessequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000703 Res = LHS.Val <= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000704 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000705 break;
706 case tok::less:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000707 Res = LHS.Val < RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000708 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000709 break;
710 case tok::greaterequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000711 Res = LHS.Val >= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000712 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000713 break;
714 case tok::greater:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000715 Res = LHS.Val > RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000716 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000717 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000718 case tok::exclaimequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000719 Res = LHS.Val != RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000720 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000721 break;
722 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000723 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000724 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000725 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000726 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000727 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000728 break;
729 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000730 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000731 break;
732 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000733 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000734 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000735 case tok::ampamp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000736 Res = (LHS.Val != 0 && RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000737 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000738 break;
739 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000740 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000741 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000742 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000743 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000744 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
745 // if not being evaluated.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000746 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000747 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
748 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000749 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000750 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000751 case tok::question: {
752 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000753 if (PeekTok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +0000754 PP.Diag(PeekTok.getLocation(), diag::err_expected)
755 << tok::colon << LHS.getRange() << RHS.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000756 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +0000757 return true;
758 }
759 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000760 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000761
762 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000763 bool AfterColonLive = ValueLive && LHS.Val == 0;
764 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000765 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000766 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
767 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000768
Chris Lattnerca2b3182008-05-05 20:09:27 +0000769 // Parse anything after the : with the same precedence as ?. We allow
770 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000771 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Chris Lattner86054a92007-04-10 05:26:38 +0000772 PeekTok, AfterColonLive, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000773 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner22eb9722006-06-18 05:43:12 +0000775 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000776 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
777 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000778
779 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
780 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000781 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner22eb9722006-06-18 05:43:12 +0000783 // Figure out the precedence of the token after the : part.
784 PeekPrec = getPrecedence(PeekTok.getKind());
785 break;
786 }
787 case tok::colon:
788 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000789 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
790 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000791 return true;
792 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000793
794 // If this operator is live and overflowed, report the issue.
795 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000796 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
797 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattner9cc755d2007-04-10 07:07:11 +0000799 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000800 LHS.Val = Res;
801 LHS.setEnd(RHS.getRange().getEnd());
Richard Smith4d247e72016-04-16 00:07:09 +0000802 RHS.setIdentifier(nullptr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000803 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000804}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000805
806/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
807/// may occur after a #if or #elif directive. If the expression is equivalent
808/// to "!defined(X)" return X in IfNDefMacro.
Yaron Keren203439f2015-05-02 19:29:29 +0000809bool Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikie1dc4a3d2013-03-18 23:22:28 +0000810 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattner4c53c402009-12-14 05:00:18 +0000811 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
812 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
813 // in which case a directive is undefined behavior. We want macros to be able
814 // to recursively expand in order to get more gcc-list behavior, so we force
815 // DisableMacroExpansion to false and restore it when we're done parsing the
816 // expression.
817 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
818 DisableMacroExpansion = false;
819
Chris Lattnere3519cc2006-07-04 18:11:39 +0000820 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000821 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000822 LexNonComment(Tok);
Douglas Gregor12785102010-08-24 20:21:13 +0000823
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000824 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000825 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattner3565c8e2008-05-05 06:45:50 +0000827 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000828 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000829 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000830 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000831 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000832 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000833
834 // Restore 'DisableMacroExpansion'.
835 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000836 return false;
837 }
Mike Stump11289f42009-09-09 15:08:12 +0000838
Chris Lattnere3519cc2006-07-04 18:11:39 +0000839 // If we are at the end of the expression after just parsing a value, there
840 // must be no (unparenthesized) binary operators involved, so we can exit
841 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000842 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000843 // If the expression we parsed was of the form !defined(macro), return the
844 // macro in IfNDefMacro.
845 if (DT.State == DefinedTracker::NotDefinedMacro)
846 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000847
Chris Lattner4c53c402009-12-14 05:00:18 +0000848 // Restore 'DisableMacroExpansion'.
849 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000850 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattnere3519cc2006-07-04 18:11:39 +0000853 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
854 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000855 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
856 Tok, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000857 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000858 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000859 DiscardUntilEndOfDirective();
Chris Lattner4c53c402009-12-14 05:00:18 +0000860
861 // Restore 'DisableMacroExpansion'.
862 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000863 return false;
864 }
Mike Stump11289f42009-09-09 15:08:12 +0000865
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000866 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000867 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000868 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000869 Diag(Tok, diag::err_pp_expected_eol);
870 DiscardUntilEndOfDirective();
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner4c53c402009-12-14 05:00:18 +0000873 // Restore 'DisableMacroExpansion'.
874 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000875 return ResVal.Val != 0;
Chris Lattnere3519cc2006-07-04 18:11:39 +0000876}