blob: ac01efad9bf69deaf6fbabc358cf8780507cc226 [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,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +000076 bool &IncludedUndefinedIds,
Chris Lattner86054a92007-04-10 05:26:38 +000077 Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000078
Chris Lattnerb9d90f72006-07-04 18:32:03 +000079/// DefinedTracker - This struct is used while parsing expressions to keep track
80/// of whether !defined(X) has been seen.
81///
82/// With this simple scheme, we handle the basic forms:
83/// !defined(X) and !defined X
84/// but we also trivially handle (silly) stuff like:
85/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
86struct DefinedTracker {
87 /// Each time a Value is evaluated, it returns information about whether the
88 /// parsed value is of the form defined(X), !defined(X) or is something else.
89 enum TrackerState {
90 DefinedMacro, // defined(X)
91 NotDefinedMacro, // !defined(X)
92 Unknown // Something else.
93 } State;
94 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
95 /// indicates the macro that was checked.
96 IdentifierInfo *TheMacro;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +000097 bool IncludedUndefinedIds = false;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000098};
99
John Thompsonb5353522009-10-30 13:49:06 +0000100/// EvaluateDefined - Process a 'defined(sym)' expression.
Chris Lattner4c53c402009-12-14 05:00:18 +0000101static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
102 bool ValueLive, Preprocessor &PP) {
John Thompsoncda95fe2013-07-19 18:50:04 +0000103 SourceLocation beginLoc(PeekTok.getLocation());
104 Result.setBegin(beginLoc);
John Thompsonb5353522009-10-30 13:49:06 +0000105
106 // Get the next token, don't expand it.
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000107 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000108
109 // Two options, it can either be a pp-identifier or a (.
110 SourceLocation LParenLoc;
111 if (PeekTok.is(tok::l_paren)) {
112 // Found a paren, remember we saw it and skip it.
113 LParenLoc = PeekTok.getLocation();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000114 PP.LexUnexpandedNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000115 }
116
Douglas Gregor12785102010-08-24 20:21:13 +0000117 if (PeekTok.is(tok::code_completion)) {
118 if (PP.getCodeCompletionHandler())
119 PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000120 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000121 PP.LexUnexpandedNonComment(PeekTok);
Douglas Gregor12785102010-08-24 20:21:13 +0000122 }
Alp Tokerb05e0b52014-05-21 06:13:51 +0000123
John Thompsonb5353522009-10-30 13:49:06 +0000124 // If we don't have a pp-identifier now, this is an error.
Serge Pavlovd024f522014-10-24 17:31:32 +0000125 if (PP.CheckMacroName(PeekTok, MU_Other))
John Thompsonb5353522009-10-30 13:49:06 +0000126 return true;
John Thompsonb5353522009-10-30 13:49:06 +0000127
128 // Otherwise, we got an identifier, is it defined to something?
Alp Tokerb05e0b52014-05-21 06:13:51 +0000129 IdentifierInfo *II = PeekTok.getIdentifierInfo();
Richard Smith66a81862015-05-04 02:25:31 +0000130 MacroDefinition Macro = PP.getMacroDefinition(II);
Richard Smith20e883e2015-04-29 23:20:19 +0000131 Result.Val = !!Macro;
Richard Smith66a81862015-05-04 02:25:31 +0000132 Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000133 DT.IncludedUndefinedIds = !Macro;
John Thompsonb5353522009-10-30 13:49:06 +0000134
135 // If there is a macro, mark it used.
Richard Smith20e883e2015-04-29 23:20:19 +0000136 if (Result.Val != 0 && ValueLive)
137 PP.markMacroAsUsed(Macro.getMacroInfo());
John Thompsonb5353522009-10-30 13:49:06 +0000138
John Thompsoncda95fe2013-07-19 18:50:04 +0000139 // Save macro token for callback.
140 Token macroToken(PeekTok);
Douglas Gregor82e0d7282011-10-14 00:49:43 +0000141
John Thompsonb5353522009-10-30 13:49:06 +0000142 // If we are in parens, ensure we have a trailing ).
143 if (LParenLoc.isValid()) {
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000144 // Consume identifier.
145 Result.setEnd(PeekTok.getLocation());
146 PP.LexUnexpandedNonComment(PeekTok);
147
John Thompsonb5353522009-10-30 13:49:06 +0000148 if (PeekTok.isNot(tok::r_paren)) {
Alp Toker751d6352013-12-30 01:59:29 +0000149 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
150 << "'defined'" << tok::r_paren;
Alp Tokerec543272013-12-24 09:48:30 +0000151 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
John Thompsonb5353522009-10-30 13:49:06 +0000152 return true;
153 }
154 // Consume the ).
155 Result.setEnd(PeekTok.getLocation());
156 PP.LexNonComment(PeekTok);
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000157 } else {
158 // Consume identifier.
159 Result.setEnd(PeekTok.getLocation());
160 PP.LexNonComment(PeekTok);
John Thompsonb5353522009-10-30 13:49:06 +0000161 }
162
Nico Weberb2348f42016-01-19 15:15:31 +0000163 // [cpp.cond]p4:
164 // Prior to evaluation, macro invocations in the list of preprocessing
165 // tokens that will become the controlling constant expression are replaced
166 // (except for those macro names modified by the 'defined' unary operator),
167 // just as in normal text. If the token 'defined' is generated as a result
168 // of this replacement process or use of the 'defined' unary operator does
169 // not match one of the two specified forms prior to macro replacement, the
170 // behavior is undefined.
171 // This isn't an idle threat, consider this program:
172 // #define FOO
173 // #define BAR defined(FOO)
174 // #if BAR
175 // ...
176 // #else
177 // ...
178 // #endif
179 // clang and gcc will pick the #if branch while Visual Studio will take the
180 // #else branch. Emit a warning about this undefined behavior.
181 if (beginLoc.isMacroID()) {
182 bool IsFunctionTypeMacro =
183 PP.getSourceManager()
184 .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
185 .getExpansion()
186 .isFunctionMacroExpansion();
187 // For object-type macros, it's easy to replace
188 // #define FOO defined(BAR)
189 // with
190 // #if defined(BAR)
191 // #define FOO 1
192 // #else
193 // #define FOO 0
194 // #endif
195 // and doing so makes sense since compilers handle this differently in
196 // practice (see example further up). But for function-type macros,
197 // there is no good way to write
198 // # define FOO(x) (defined(M_ ## x) && M_ ## x)
199 // in a different way, and compilers seem to agree on how to behave here.
200 // So warn by default on object-type macros, but only warn in -pedantic
201 // mode on function-type macros.
202 if (IsFunctionTypeMacro)
203 PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
204 else
205 PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
206 }
207
John Thompsoncda95fe2013-07-19 18:50:04 +0000208 // Invoke the 'defined' callback.
209 if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
Richard Smith36bd40d2015-05-04 03:15:40 +0000210 Callbacks->Defined(macroToken, Macro,
John Thompsoncda95fe2013-07-19 18:50:04 +0000211 SourceRange(beginLoc, PeekTok.getLocation()));
212 }
213
John Thompsonb5353522009-10-30 13:49:06 +0000214 // Success, remember that we saw defined(X).
215 DT.State = DefinedTracker::DefinedMacro;
216 DT.TheMacro = II;
217 return false;
218}
219
Chris Lattner22eb9722006-06-18 05:43:12 +0000220/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
221/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000222/// parsing. This function also returns information about the form of the
223/// expression in DT. See above for information on what DT means.
Chris Lattner86054a92007-04-10 05:26:38 +0000224///
225/// If ValueLive is false, then this value is being evaluated in a context where
226/// the result is not used. As such, avoid diagnostics that relate to
227/// evaluation.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000228static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
229 bool ValueLive, Preprocessor &PP) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000230 DT.State = DefinedTracker::Unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000231
Richard Smith4d247e72016-04-16 00:07:09 +0000232 Result.setIdentifier(nullptr);
233
Douglas Gregorec00a262010-08-24 22:20:20 +0000234 if (PeekTok.is(tok::code_completion)) {
235 if (PP.getCodeCompletionHandler())
236 PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000237 PP.setCodeCompletionReached();
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000238 PP.LexNonComment(PeekTok);
Douglas Gregorec00a262010-08-24 22:20:20 +0000239 }
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner22eb9722006-06-18 05:43:12 +0000241 switch (PeekTok.getKind()) {
Olivier Goffart90f981b2017-07-14 09:23:40 +0000242 default:
243 // If this token's spelling is a pp-identifier, check to see if it is
244 // 'defined' or if it is a macro. Note that we check here because many
245 // keywords are pp-identifiers, so we can't check the kind.
246 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
247 // Handle "defined X" and "defined(X)".
248 if (II->isStr("defined"))
249 return EvaluateDefined(Result, PeekTok, DT, ValueLive, PP);
250
251 if (!II->isCPlusPlusOperatorKeyword()) {
252 // If this identifier isn't 'defined' or one of the special
253 // preprocessor keywords and it wasn't macro expanded, it turns
254 // into a simple 0
255 if (ValueLive)
256 PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
257 Result.Val = 0;
258 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
259 Result.setIdentifier(II);
260 Result.setRange(PeekTok.getLocation());
261 DT.IncludedUndefinedIds = true;
262 PP.LexNonComment(PeekTok);
263 return false;
264 }
265 }
Chris Lattnerf8f94542008-04-13 20:38:43 +0000266 PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000267 return true;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000268 case tok::eod:
Chris Lattner22eb9722006-06-18 05:43:12 +0000269 case tok::r_paren:
270 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000271 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000272 return true;
273 case tok::numeric_constant: {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000274 SmallString<64> IntegerBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000275 bool NumberInvalid = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000276 StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000277 &NumberInvalid);
278 if (NumberInvalid)
279 return true; // a diagnostic was already reported
280
Dmitri Gribenko7ba91722012-09-24 09:53:54 +0000281 NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000282 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000283 return true; // a diagnostic was already reported.
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattnered045422007-08-26 03:29:23 +0000285 if (Literal.isFloatingLiteral() || Literal.isImaginary) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000286 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000287 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000288 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000289 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000290
Richard Smith39570d002012-03-08 08:45:32 +0000291 // Complain about, and drop, any ud-suffix.
292 if (Literal.hasUDSuffix())
293 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
294
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000295 // 'long long' is a C99 or C++11 feature.
296 if (!PP.getLangOpts().C99 && Literal.isLongLong) {
297 if (PP.getLangOpts().CPlusPlus)
298 PP.Diag(PeekTok,
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000299 PP.getLangOpts().CPlusPlus11 ?
Dmitri Gribenko1cd23052012-09-24 18:19:21 +0000300 diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
301 else
302 PP.Diag(PeekTok, diag::ext_c99_longlong);
303 }
Neil Boothac582c52007-08-29 22:00:19 +0000304
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000305 // Parse the integer literal into Result.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000306 if (Literal.GetIntegerValue(Result.Val)) {
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000307 // Overflow parsing integer literal.
Aaron Ballman446867e2014-07-22 14:08:09 +0000308 if (ValueLive)
Aaron Ballman31f42312014-07-24 14:51:23 +0000309 PP.Diag(PeekTok, diag::err_integer_literal_too_large)
310 << /* Unsigned */ 1;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000311 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000312 } else {
313 // Set the signedness of the result to match whether there was a U suffix
314 // or not.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000315 Result.Val.setIsUnsigned(Literal.isUnsigned);
Mike Stump11289f42009-09-09 15:08:12 +0000316
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000317 // Detect overflow based on whether the value is signed. If signed
318 // and if the value is too large, emit a warning "integer constant is so
319 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
320 // is 64-bits.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000321 if (!Literal.isUnsigned && Result.Val.isNegative()) {
Richard Smith7e34fbd2014-03-14 21:21:24 +0000322 // Octal, hexadecimal, and binary literals are implicitly unsigned if
323 // the value does not fit into a signed integer type.
Eli Friedman088d39a2013-07-23 00:25:18 +0000324 if (ValueLive && Literal.getRadix() == 10)
Aaron Ballman31f42312014-07-24 14:51:23 +0000325 PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000326 Result.Val.setIsUnsigned(true);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000327 }
328 }
Mike Stump11289f42009-09-09 15:08:12 +0000329
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000330 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000331 Result.setRange(PeekTok.getLocation());
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000332 PP.LexNonComment(PeekTok);
333 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000334 }
Douglas Gregorfb65e592011-07-27 05:40:30 +0000335 case tok::char_constant: // 'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000336 case tok::wide_char_constant: // L'x'
Richard Smith3e3a7052014-11-08 06:08:42 +0000337 case tok::utf8_char_constant: // u8'x'
Douglas Gregorfb65e592011-07-27 05:40:30 +0000338 case tok::utf16_char_constant: // u'x'
Alexander Kornienko2a603662013-01-31 19:03:16 +0000339 case tok::utf32_char_constant: { // U'x'
Richard Smithd67aea22012-03-06 03:21:47 +0000340 // Complain about, and drop, any ud-suffix.
341 if (PeekTok.hasUDSuffix())
Richard Smith39570d002012-03-08 08:45:32 +0000342 PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
Richard Smithd67aea22012-03-06 03:21:47 +0000343
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000344 SmallString<32> CharBuffer;
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000345 bool CharInvalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000346 StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
Douglas Gregor7bda4b82010-03-16 05:20:39 +0000347 if (CharInvalid)
348 return true;
Benjamin Kramer0a1abd42010-02-27 13:44:12 +0000349
350 CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
Douglas Gregorfb65e592011-07-27 05:40:30 +0000351 PeekTok.getLocation(), PP, PeekTok.getKind());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000352 if (Literal.hadError())
353 return true; // A diagnostic was already emitted.
354
355 // Character literals are always int or wchar_t, expand to intmax_t.
Daniel Dunbar1b444192009-11-13 05:51:54 +0000356 const TargetInfo &TI = PP.getTargetInfo();
Eli Friedmand8cec572009-06-01 05:25:02 +0000357 unsigned NumBits;
358 if (Literal.isMultiChar())
359 NumBits = TI.getIntWidth();
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000360 else if (Literal.isWide())
361 NumBits = TI.getWCharWidth();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000362 else if (Literal.isUTF16())
363 NumBits = TI.getChar16Width();
364 else if (Literal.isUTF32())
365 NumBits = TI.getChar32Width();
Richard Smith3a8244d2018-05-01 05:02:45 +0000366 else // char or char8_t
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000367 NumBits = TI.getCharWidth();
Eli Friedmand8cec572009-06-01 05:25:02 +0000368
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000369 // Set the width.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000370 llvm::APSInt Val(NumBits);
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000371 // Set the value.
372 Val = Literal.getValue();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000373 // Set the signedness. UTF-16 and UTF-32 are always unsigned
Michael Wong867eeb52015-02-24 13:34:20 +0000374 if (Literal.isWide())
375 Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
376 else if (!Literal.isUTF16() && !Literal.isUTF32())
David Blaikiebbafb8a2012-03-11 07:00:24 +0000377 Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattner3565c8e2008-05-05 06:45:50 +0000379 if (Result.Val.getBitWidth() > Val.getBitWidth()) {
380 Result.Val = Val.extend(Result.Val.getBitWidth());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000381 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000382 assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000383 "intmax_t smaller than char/wchar_t?");
Chris Lattner3565c8e2008-05-05 06:45:50 +0000384 Result.Val = Val;
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000385 }
386
387 // Consume the token.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000388 Result.setRange(PeekTok.getLocation());
Chris Lattnerf8a0b0f2007-04-05 06:58:56 +0000389 PP.LexNonComment(PeekTok);
390 return false;
391 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000392 case tok::l_paren: {
393 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000394 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000395 // Parse the value and if there are any binary operators involved, parse
396 // them.
Chris Lattner86054a92007-04-10 05:26:38 +0000397 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000398
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000399 // If this is a silly value like (X), which doesn't need parens, check for
400 // !(defined X).
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000401 if (PeekTok.is(tok::r_paren)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000402 // Just use DT unmodified as our result.
403 } else {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000404 // Otherwise, we have something like (x+y), and we consumed '(x'.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000405 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, ValueLive,
406 DT.IncludedUndefinedIds, PP))
Chris Lattner86054a92007-04-10 05:26:38 +0000407 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000409 if (PeekTok.isNot(tok::r_paren)) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000410 PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
411 << Result.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000412 PP.Diag(Start, diag::note_matching) << tok::l_paren;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000413 return true;
414 }
415 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000416 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000417 Result.setRange(Start, PeekTok.getLocation());
Richard Smith4d247e72016-04-16 00:07:09 +0000418 Result.setIdentifier(nullptr);
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000419 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000420 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000421 }
422 case tok::plus: {
423 SourceLocation Start = PeekTok.getLocation();
Chris Lattner22eb9722006-06-18 05:43:12 +0000424 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000425 PP.LexNonComment(PeekTok);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000426 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
427 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000428 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000429 return false;
430 }
Chris Lattner7e61ac52007-04-11 03:42:36 +0000431 case tok::minus: {
432 SourceLocation Loc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000433 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000434 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000435 Result.setBegin(Loc);
Richard Smith4d247e72016-04-16 00:07:09 +0000436 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000438 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000439 Result.Val = -Result.Val;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner1cb0e612008-07-03 03:47:30 +0000441 // -MININT is the only thing that overflows. Unsigned never overflows.
442 bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner7e61ac52007-04-11 03:42:36 +0000444 // If this operator is live and overflowed, report the issue.
445 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000446 PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000447
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000448 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000449 return false;
Chris Lattner7e61ac52007-04-11 03:42:36 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner3565c8e2008-05-05 06:45:50 +0000452 case tok::tilde: {
453 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000454 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000455 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000456 Result.setBegin(Start);
Richard Smith4d247e72016-04-16 00:07:09 +0000457 Result.setIdentifier(nullptr);
Chris Lattner3565c8e2008-05-05 06:45:50 +0000458
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000459 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000460 Result.Val = ~Result.Val;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000461 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000462 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000463 }
Mike Stump11289f42009-09-09 15:08:12 +0000464
Chris Lattner3565c8e2008-05-05 06:45:50 +0000465 case tok::exclaim: {
466 SourceLocation Start = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000467 PP.LexNonComment(PeekTok);
Chris Lattner86054a92007-04-10 05:26:38 +0000468 if (EvaluateValue(Result, PeekTok, DT, ValueLive, PP)) return true;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000469 Result.setBegin(Start);
470 Result.Val = !Result.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000471 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000472 Result.Val.setIsUnsigned(false);
Richard Smith4d247e72016-04-16 00:07:09 +0000473 Result.setIdentifier(nullptr);
Mike Stump11289f42009-09-09 15:08:12 +0000474
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000475 if (DT.State == DefinedTracker::DefinedMacro)
476 DT.State = DefinedTracker::NotDefinedMacro;
477 else if (DT.State == DefinedTracker::NotDefinedMacro)
478 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000479 return false;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000480 }
Olivier Goffart90f981b2017-07-14 09:23:40 +0000481 case tok::kw_true:
482 case tok::kw_false:
483 Result.Val = PeekTok.getKind() == tok::kw_true;
484 Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
485 Result.setIdentifier(PeekTok.getIdentifierInfo());
486 Result.setRange(PeekTok.getLocation());
487 PP.LexNonComment(PeekTok);
488 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000489
Chris Lattner22eb9722006-06-18 05:43:12 +0000490 // FIXME: Handle #assert
491 }
492}
493
Chris Lattner22eb9722006-06-18 05:43:12 +0000494/// getPrecedence - Return the precedence of the specified binary operator
495/// token. This returns:
496/// ~0 - Invalid token.
Chris Lattner3c57f7e2008-05-05 04:10:51 +0000497/// 14 -> 3 - various operators.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000498/// 0 - 'eod' or ')'
Chris Lattner22eb9722006-06-18 05:43:12 +0000499static unsigned getPrecedence(tok::TokenKind Kind) {
500 switch (Kind) {
501 default: return ~0U;
502 case tok::percent:
503 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000504 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000505 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000506 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000507 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000508 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000509 case tok::lessequal:
510 case tok::less:
511 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000512 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000513 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000514 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000515 case tok::amp: return 9;
516 case tok::caret: return 8;
517 case tok::pipe: return 7;
518 case tok::ampamp: return 6;
519 case tok::pipepipe: return 5;
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000520 case tok::question: return 4;
521 case tok::comma: return 3;
Chris Lattnerd89e4582008-05-04 18:36:18 +0000522 case tok::colon: return 2;
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000523 case tok::r_paren: return 0;// Lowest priority, end of expr.
524 case tok::eod: return 0;// Lowest priority, end of directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000525 }
526}
527
Richard Smith4d247e72016-04-16 00:07:09 +0000528static void diagnoseUnexpectedOperator(Preprocessor &PP, PPValue &LHS,
529 Token &Tok) {
530 if (Tok.is(tok::l_paren) && LHS.getIdentifier())
531 PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
532 << LHS.getIdentifier();
533 else
534 PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
535 << LHS.getRange();
536}
Chris Lattner22eb9722006-06-18 05:43:12 +0000537
538/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
Chris Lattner3565c8e2008-05-05 06:45:50 +0000539/// PeekTok, and whose precedence is PeekPrec. This returns the result in LHS.
Chris Lattner86054a92007-04-10 05:26:38 +0000540///
541/// If ValueLive is false, then this value is being evaluated in a context where
542/// the result is not used. As such, avoid diagnostics that relate to
Chris Lattner3565c8e2008-05-05 06:45:50 +0000543/// evaluation, such as division by zero warnings.
544static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Chris Lattner146762e2007-07-20 16:59:19 +0000545 Token &PeekTok, bool ValueLive,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000546 bool &IncludedUndefinedIds,
Chris Lattner86054a92007-04-10 05:26:38 +0000547 Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000548 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
549 // If this token isn't valid, report the error.
550 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000551 diagnoseUnexpectedOperator(PP, LHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000552 return true;
553 }
Mike Stump11289f42009-09-09 15:08:12 +0000554
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000555 while (true) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000556 // If this token has a lower precedence than we are allowed to parse, return
557 // it so that higher levels of the recursion can parse it.
558 if (PeekPrec < MinPrec)
559 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner22eb9722006-06-18 05:43:12 +0000561 tok::TokenKind Operator = PeekTok.getKind();
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattner86054a92007-04-10 05:26:38 +0000563 // If this is a short-circuiting operator, see if the RHS of the operator is
Mike Stump11289f42009-09-09 15:08:12 +0000564 // dead. Note that this cannot just clobber ValueLive. Consider
Chris Lattner86054a92007-04-10 05:26:38 +0000565 // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)". In
566 // this example, the RHS of the && being dead does not make the rest of the
567 // expr dead.
568 bool RHSIsLive;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000569 if (Operator == tok::ampamp && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000570 RHSIsLive = false; // RHS of "0 && x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000571 else if (Operator == tok::pipepipe && LHS.Val != 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000572 RHSIsLive = false; // RHS of "1 || x" is dead.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000573 else if (Operator == tok::question && LHS.Val == 0)
Chris Lattner86054a92007-04-10 05:26:38 +0000574 RHSIsLive = false; // RHS (x) of "0 ? x : y" is dead.
575 else
576 RHSIsLive = ValueLive;
Chris Lattner22eb9722006-06-18 05:43:12 +0000577
Chris Lattner3565c8e2008-05-05 06:45:50 +0000578 // Consume the operator, remembering the operator's location for reporting.
579 SourceLocation OpLoc = PeekTok.getLocation();
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000580 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000581
Chris Lattner3565c8e2008-05-05 06:45:50 +0000582 PPValue RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000583 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000584 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000585 if (EvaluateValue(RHS, PeekTok, DT, RHSIsLive, PP)) return true;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000586 IncludedUndefinedIds = DT.IncludedUndefinedIds;
Chris Lattner22eb9722006-06-18 05:43:12 +0000587
588 // Remember the precedence of this operator and get the precedence of the
589 // operator immediately to the right of the RHS.
590 unsigned ThisPrec = PeekPrec;
591 PeekPrec = getPrecedence(PeekTok.getKind());
592
593 // If this token isn't valid, report the error.
594 if (PeekPrec == ~0U) {
Richard Smith4d247e72016-04-16 00:07:09 +0000595 diagnoseUnexpectedOperator(PP, RHS, PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000596 return true;
597 }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000599 // Decide whether to include the next binop in this subexpression. For
600 // example, when parsing x+y*z and looking at '*', we want to recursively
Chris Lattnerca2b3182008-05-05 20:09:27 +0000601 // handle y*z as a single subexpression. We do this because the precedence
602 // of * is higher than that of +. The only strange case we have to handle
603 // here is for the ?: operator, where the precedence is actually lower than
604 // the LHS of the '?'. The grammar rule is:
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000605 //
606 // conditional-expression ::=
607 // logical-OR-expression ? expression : conditional-expression
608 // where 'expression' is actually comma-expression.
609 unsigned RHSPrec;
610 if (Operator == tok::question)
611 // The RHS of "?" should be maximally consumed as an expression.
612 RHSPrec = getPrecedence(tok::comma);
613 else // All others should munch while higher precedence.
614 RHSPrec = ThisPrec+1;
Mike Stump11289f42009-09-09 15:08:12 +0000615
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000616 if (PeekPrec >= RHSPrec) {
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000617 if (EvaluateDirectiveSubExpr(RHS, RHSPrec, PeekTok, RHSIsLive,
618 IncludedUndefinedIds, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000619 return true;
620 PeekPrec = getPrecedence(PeekTok.getKind());
621 }
622 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
Mike Stump11289f42009-09-09 15:08:12 +0000623
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000624 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
Mike Stump11289f42009-09-09 15:08:12 +0000625 // either operand is unsigned.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000626 llvm::APSInt Res(LHS.getBitWidth());
Chris Lattnerca671b02008-05-04 23:46:17 +0000627 switch (Operator) {
628 case tok::question: // No UAC for x and y in "x ? y : z".
629 case tok::lessless: // Shift amount doesn't UAC with shift value.
630 case tok::greatergreater: // Shift amount doesn't UAC with shift value.
631 case tok::comma: // Comma operands are not subject to UACs.
632 case tok::pipepipe: // Logical || does not do UACs.
633 case tok::ampamp: // Logical && does not do UACs.
634 break; // No UAC
635 default:
Chris Lattner99ca0912007-04-11 04:14:45 +0000636 Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
637 // If this just promoted something from signed to unsigned, and if the
638 // value was negative, warn about it.
639 if (ValueLive && Res.isUnsigned()) {
Chris Lattner3565c8e2008-05-05 06:45:50 +0000640 if (!LHS.isUnsigned() && LHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000641 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
Chris Lattnere05c4df2008-11-18 21:48:13 +0000642 << LHS.Val.toString(10, true) + " to " +
643 LHS.Val.toString(10, false)
644 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000645 if (!RHS.isUnsigned() && RHS.Val.isNegative())
Craig Topper7f5ff212015-11-14 02:09:55 +0000646 PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
Chris Lattnere05c4df2008-11-18 21:48:13 +0000647 << RHS.Val.toString(10, true) + " to " +
648 RHS.Val.toString(10, false)
649 << LHS.getRange() << RHS.getRange();
Chris Lattner99ca0912007-04-11 04:14:45 +0000650 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000651 LHS.Val.setIsUnsigned(Res.isUnsigned());
652 RHS.Val.setIsUnsigned(Res.isUnsigned());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000653 }
Mike Stump11289f42009-09-09 15:08:12 +0000654
Chris Lattner5a0f1642007-04-10 06:54:33 +0000655 bool Overflow = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000656 switch (Operator) {
David Blaikie83d382b2011-09-23 05:06:16 +0000657 default: llvm_unreachable("Unknown operator token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000658 case tok::percent:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000659 if (RHS.Val != 0)
660 Res = LHS.Val % RHS.Val;
661 else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000662 PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
663 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000664 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000665 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000666 break;
667 case tok::slash:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000668 if (RHS.Val != 0) {
Chris Lattner2edb9262010-10-13 23:46:56 +0000669 if (LHS.Val.isSigned())
670 Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
671 else
672 Res = LHS.Val / RHS.Val;
Chris Lattner3565c8e2008-05-05 06:45:50 +0000673 } else if (ValueLive) {
Chris Lattnere05c4df2008-11-18 21:48:13 +0000674 PP.Diag(OpLoc, diag::err_pp_division_by_zero)
675 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000676 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000677 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000678 break;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000680 case tok::star:
Chris Lattner2edb9262010-10-13 23:46:56 +0000681 if (Res.isSigned())
682 Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
683 else
684 Res = LHS.Val * RHS.Val;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000685 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000686 case tok::lessless: {
687 // Determine whether overflow is about to happen.
David Majnemer3e8b6ac2014-10-13 22:18:22 +0000688 if (LHS.isUnsigned())
689 Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
690 else
691 Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000692 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000693 }
694 case tok::greatergreater: {
695 // Determine whether overflow is about to happen.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000696 unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
Richard Trieucc3949d2016-02-18 22:34:54 +0000697 if (ShAmt >= LHS.getBitWidth()) {
698 Overflow = true;
699 ShAmt = LHS.getBitWidth()-1;
700 }
Chris Lattner3565c8e2008-05-05 06:45:50 +0000701 Res = LHS.Val >> ShAmt;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000702 break;
Chris Lattner5a0f1642007-04-10 06:54:33 +0000703 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000704 case tok::plus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000705 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000706 Res = LHS.Val + RHS.Val;
707 else
708 Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000709 break;
710 case tok::minus:
Chris Lattner028c7de2007-04-11 03:34:29 +0000711 if (LHS.isUnsigned())
Chris Lattner2edb9262010-10-13 23:46:56 +0000712 Res = LHS.Val - RHS.Val;
713 else
714 Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000715 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000716 case tok::lessequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000717 Res = LHS.Val <= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000718 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000719 break;
720 case tok::less:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000721 Res = LHS.Val < RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000722 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000723 break;
724 case tok::greaterequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000725 Res = LHS.Val >= RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000726 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000727 break;
728 case tok::greater:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000729 Res = LHS.Val > RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000730 Res.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000731 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000732 case tok::exclaimequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000733 Res = LHS.Val != RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000734 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000735 break;
736 case tok::equalequal:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000737 Res = LHS.Val == RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000738 Res.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000739 break;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000740 case tok::amp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000741 Res = LHS.Val & RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000742 break;
743 case tok::caret:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000744 Res = LHS.Val ^ RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000745 break;
746 case tok::pipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000747 Res = LHS.Val | RHS.Val;
Chris Lattner9cc755d2007-04-10 07:07:11 +0000748 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000749 case tok::ampamp:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000750 Res = (LHS.Val != 0 && RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000751 Res.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000752 break;
753 case tok::pipepipe:
Chris Lattner3565c8e2008-05-05 06:45:50 +0000754 Res = (LHS.Val != 0 || RHS.Val != 0);
Chris Lattner9cc755d2007-04-10 07:07:11 +0000755 Res.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000756 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000757 case tok::comma:
Chris Lattnerd89e4582008-05-04 18:36:18 +0000758 // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
759 // if not being evaluated.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000760 if (!PP.getLangOpts().C99 || ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000761 PP.Diag(OpLoc, diag::ext_pp_comma_expr)
762 << LHS.getRange() << RHS.getRange();
Chris Lattner3565c8e2008-05-05 06:45:50 +0000763 Res = RHS.Val; // LHS = LHS,RHS -> RHS.
Mike Stump11289f42009-09-09 15:08:12 +0000764 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000765 case tok::question: {
766 // Parse the : part of the expression.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000767 if (PeekTok.isNot(tok::colon)) {
Alp Toker35d87032013-12-30 23:29:50 +0000768 PP.Diag(PeekTok.getLocation(), diag::err_expected)
769 << tok::colon << LHS.getRange() << RHS.getRange();
Alp Tokerec543272013-12-24 09:48:30 +0000770 PP.Diag(OpLoc, diag::note_matching) << tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +0000771 return true;
772 }
773 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000774 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000775
776 // Evaluate the value after the :.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000777 bool AfterColonLive = ValueLive && LHS.Val == 0;
778 PPValue AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000779 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000780 if (EvaluateValue(AfterColonVal, PeekTok, DT, AfterColonLive, PP))
781 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000782
Chris Lattnerca2b3182008-05-05 20:09:27 +0000783 // Parse anything after the : with the same precedence as ?. We allow
784 // things of equal precedence because ?: is right associative.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000785 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec,
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000786 PeekTok, AfterColonLive,
787 IncludedUndefinedIds, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000788 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000789
Chris Lattner22eb9722006-06-18 05:43:12 +0000790 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000791 Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
792 RHS.setEnd(AfterColonVal.getRange().getEnd());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000793
794 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
795 // either operand is unsigned.
Chris Lattner9cc755d2007-04-10 07:07:11 +0000796 Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Mike Stump11289f42009-09-09 15:08:12 +0000797
Chris Lattner22eb9722006-06-18 05:43:12 +0000798 // Figure out the precedence of the token after the : part.
799 PeekPrec = getPrecedence(PeekTok.getKind());
800 break;
801 }
802 case tok::colon:
803 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere05c4df2008-11-18 21:48:13 +0000804 PP.Diag(OpLoc, diag::err_pp_colon_without_question)
805 << LHS.getRange() << RHS.getRange();
Chris Lattner22eb9722006-06-18 05:43:12 +0000806 return true;
807 }
Chris Lattner5a0f1642007-04-10 06:54:33 +0000808
809 // If this operator is live and overflowed, report the issue.
810 if (Overflow && ValueLive)
Chris Lattnere05c4df2008-11-18 21:48:13 +0000811 PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
812 << LHS.getRange() << RHS.getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000813
Chris Lattner9cc755d2007-04-10 07:07:11 +0000814 // Put the result back into 'LHS' for our next iteration.
Chris Lattner3565c8e2008-05-05 06:45:50 +0000815 LHS.Val = Res;
816 LHS.setEnd(RHS.getRange().getEnd());
Richard Smith4d247e72016-04-16 00:07:09 +0000817 RHS.setIdentifier(nullptr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000818 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000819}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000820
821/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
822/// may occur after a #if or #elif directive. If the expression is equivalent
823/// to "!defined(X)" return X in IfNDefMacro.
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000824Preprocessor::DirectiveEvalResult
825Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
David Blaikie1dc4a3d2013-03-18 23:22:28 +0000826 SaveAndRestore<bool> PPDir(ParsingIfOrElifDirective, true);
Chris Lattner4c53c402009-12-14 05:00:18 +0000827 // Save the current state of 'DisableMacroExpansion' and reset it to false. If
828 // 'DisableMacroExpansion' is true, then we must be in a macro argument list
829 // in which case a directive is undefined behavior. We want macros to be able
830 // to recursively expand in order to get more gcc-list behavior, so we force
831 // DisableMacroExpansion to false and restore it when we're done parsing the
832 // expression.
833 bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
834 DisableMacroExpansion = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000835
Chris Lattnere3519cc2006-07-04 18:11:39 +0000836 // Peek ahead one token.
Chris Lattner146762e2007-07-20 16:59:19 +0000837 Token Tok;
Eli Friedmanb3bfd842011-08-03 00:04:13 +0000838 LexNonComment(Tok);
Fangrui Song6907ce22018-07-30 19:24:48 +0000839
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000840 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
Chris Lattner37e05872008-03-05 18:54:05 +0000841 unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattner3565c8e2008-05-05 06:45:50 +0000843 PPValue ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000844 DefinedTracker DT;
Chris Lattner86054a92007-04-10 05:26:38 +0000845 if (EvaluateValue(ResVal, Tok, DT, true, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000846 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000847 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000848 DiscardUntilEndOfDirective();
Fangrui Song6907ce22018-07-30 19:24:48 +0000849
Chris Lattner4c53c402009-12-14 05:00:18 +0000850 // Restore 'DisableMacroExpansion'.
851 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000852 return {false, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Chris Lattnere3519cc2006-07-04 18:11:39 +0000855 // If we are at the end of the expression after just parsing a value, there
856 // must be no (unparenthesized) binary operators involved, so we can exit
857 // directly.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000858 if (Tok.is(tok::eod)) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000859 // If the expression we parsed was of the form !defined(macro), return the
860 // macro in IfNDefMacro.
861 if (DT.State == DefinedTracker::NotDefinedMacro)
862 IfNDefMacro = DT.TheMacro;
Mike Stump11289f42009-09-09 15:08:12 +0000863
Chris Lattner4c53c402009-12-14 05:00:18 +0000864 // Restore 'DisableMacroExpansion'.
865 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000866 return {ResVal.Val != 0, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000867 }
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattnere3519cc2006-07-04 18:11:39 +0000869 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
870 // operator and the stuff after it.
Chris Lattnerdb65ff72008-05-05 20:07:41 +0000871 if (EvaluateDirectiveSubExpr(ResVal, getPrecedence(tok::question),
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000872 Tok, true, DT.IncludedUndefinedIds, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000873 // Parse error, skip the rest of the macro line.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000874 if (Tok.isNot(tok::eod))
Chris Lattnere3519cc2006-07-04 18:11:39 +0000875 DiscardUntilEndOfDirective();
Fangrui Song6907ce22018-07-30 19:24:48 +0000876
Chris Lattner4c53c402009-12-14 05:00:18 +0000877 // Restore 'DisableMacroExpansion'.
878 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000879 return {false, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000880 }
Mike Stump11289f42009-09-09 15:08:12 +0000881
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000882 // If we aren't at the tok::eod token, something bad happened, like an extra
Chris Lattnere3519cc2006-07-04 18:11:39 +0000883 // ')' token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000884 if (Tok.isNot(tok::eod)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000885 Diag(Tok, diag::err_pp_expected_eol);
886 DiscardUntilEndOfDirective();
887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Chris Lattner4c53c402009-12-14 05:00:18 +0000889 // Restore 'DisableMacroExpansion'.
890 DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
Argyrios Kyrtzidisad870f82017-06-20 14:36:58 +0000891 return {ResVal.Val != 0, DT.IncludedUndefinedIds};
Chris Lattnere3519cc2006-07-04 18:11:39 +0000892}