Clang Project

clang_source_code/lib/Lex/PPExpressions.cpp
1//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Preprocessor::EvaluateDirectiveExpression method,
10// which parses and evaluates integer constant expressions for #if directives.
11//
12//===----------------------------------------------------------------------===//
13//
14// FIXME: implement testing for #assert's.
15//
16//===----------------------------------------------------------------------===//
17
18#include "clang/Lex/Preprocessor.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/SourceLocation.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/TokenKinds.h"
24#include "clang/Lex/CodeCompletionHandler.h"
25#include "clang/Lex/LexDiagnostic.h"
26#include "clang/Lex/LiteralSupport.h"
27#include "clang/Lex/MacroInfo.h"
28#include "clang/Lex/PPCallbacks.h"
29#include "clang/Lex/Token.h"
30#include "llvm/ADT/APSInt.h"
31#include "llvm/ADT/SmallString.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/SaveAndRestore.h"
35#include <cassert>
36
37using namespace clang;
38
39namespace {
40
41/// PPValue - Represents the value of a subexpression of a preprocessor
42/// conditional and the source range covered by it.
43class PPValue {
44  SourceRange Range;
45  IdentifierInfo *II;
46
47public:
48  llvm::APSInt Val;
49
50  // Default ctor - Construct an 'invalid' PPValue.
51  PPValue(unsigned BitWidth) : Val(BitWidth) {}
52
53  // If this value was produced by directly evaluating an identifier, produce
54  // that identifier.
55  IdentifierInfo *getIdentifier() const { return II; }
56  void setIdentifier(IdentifierInfo *II) { this->II = II; }
57
58  unsigned getBitWidth() const { return Val.getBitWidth(); }
59  bool isUnsigned() const { return Val.isUnsigned(); }
60
61  SourceRange getRange() const { return Range; }
62
63  void setRange(SourceLocation L) { Range.setBegin(L); Range.setEnd(L); }
64  void setRange(SourceLocation BSourceLocation E) {
65    Range.setBegin(B); Range.setEnd(E);
66  }
67  void setBegin(SourceLocation L) { Range.setBegin(L); }
68  void setEnd(SourceLocation L) { Range.setEnd(L); }
69};
70
71// end anonymous namespace
72
73static bool EvaluateDirectiveSubExpr(PPValue &LHSunsigned MinPrec,
74                                     Token &PeekTokbool ValueLive,
75                                     bool &IncludedUndefinedIds,
76                                     Preprocessor &PP);
77
78/// 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  bool IncludedUndefinedIds = false;
97};
98
99/// EvaluateDefined - Process a 'defined(sym)' expression.
100static bool EvaluateDefined(PPValue &ResultToken &PeekTokDefinedTracker &DT,
101                            bool ValueLivePreprocessor &PP) {
102  SourceLocation beginLoc(PeekTok.getLocation());
103  Result.setBegin(beginLoc);
104
105  // Get the next token, don't expand it.
106  PP.LexUnexpandedNonComment(PeekTok);
107
108  // Two options, it can either be a pp-identifier or a (.
109  SourceLocation LParenLoc;
110  if (PeekTok.is(tok::l_paren)) {
111    // Found a paren, remember we saw it and skip it.
112    LParenLoc = PeekTok.getLocation();
113    PP.LexUnexpandedNonComment(PeekTok);
114  }
115
116  if (PeekTok.is(tok::code_completion)) {
117    if (PP.getCodeCompletionHandler())
118      PP.getCodeCompletionHandler()->CodeCompleteMacroName(false);
119    PP.setCodeCompletionReached();
120    PP.LexUnexpandedNonComment(PeekTok);
121  }
122
123  // If we don't have a pp-identifier now, this is an error.
124  if (PP.CheckMacroName(PeekTokMU_Other))
125    return true;
126
127  // Otherwise, we got an identifier, is it defined to something?
128  IdentifierInfo *II = PeekTok.getIdentifierInfo();
129  MacroDefinition Macro = PP.getMacroDefinition(II);
130  Result.Val = !!Macro;
131  Result.Val.setIsUnsigned(false); // Result is signed intmax_t.
132  DT.IncludedUndefinedIds = !Macro;
133
134  // If there is a macro, mark it used.
135  if (Result.Val != 0 && ValueLive)
136    PP.markMacroAsUsed(Macro.getMacroInfo());
137
138  // Save macro token for callback.
139  Token macroToken(PeekTok);
140
141  // If we are in parens, ensure we have a trailing ).
142  if (LParenLoc.isValid()) {
143    // Consume identifier.
144    Result.setEnd(PeekTok.getLocation());
145    PP.LexUnexpandedNonComment(PeekTok);
146
147    if (PeekTok.isNot(tok::r_paren)) {
148      PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_after)
149          << "'defined'" << tok::r_paren;
150      PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
151      return true;
152    }
153    // Consume the ).
154    PP.LexNonComment(PeekTok);
155    Result.setEnd(PeekTok.getLocation());
156  } else {
157    // Consume identifier.
158    Result.setEnd(PeekTok.getLocation());
159    PP.LexNonComment(PeekTok);
160  }
161
162  // [cpp.cond]p4:
163  //   Prior to evaluation, macro invocations in the list of preprocessing
164  //   tokens that will become the controlling constant expression are replaced
165  //   (except for those macro names modified by the 'defined' unary operator),
166  //   just as in normal text. If the token 'defined' is generated as a result
167  //   of this replacement process or use of the 'defined' unary operator does
168  //   not match one of the two specified forms prior to macro replacement, the
169  //   behavior is undefined.
170  // This isn't an idle threat, consider this program:
171  //   #define FOO
172  //   #define BAR defined(FOO)
173  //   #if BAR
174  //   ...
175  //   #else
176  //   ...
177  //   #endif
178  // clang and gcc will pick the #if branch while Visual Studio will take the
179  // #else branch.  Emit a warning about this undefined behavior.
180  if (beginLoc.isMacroID()) {
181    bool IsFunctionTypeMacro =
182        PP.getSourceManager()
183            .getSLocEntry(PP.getSourceManager().getFileID(beginLoc))
184            .getExpansion()
185            .isFunctionMacroExpansion();
186    // For object-type macros, it's easy to replace
187    //   #define FOO defined(BAR)
188    // with
189    //   #if defined(BAR)
190    //   #define FOO 1
191    //   #else
192    //   #define FOO 0
193    //   #endif
194    // and doing so makes sense since compilers handle this differently in
195    // practice (see example further up).  But for function-type macros,
196    // there is no good way to write
197    //   # define FOO(x) (defined(M_ ## x) && M_ ## x)
198    // in a different way, and compilers seem to agree on how to behave here.
199    // So warn by default on object-type macros, but only warn in -pedantic
200    // mode on function-type macros.
201    if (IsFunctionTypeMacro)
202      PP.Diag(beginLoc, diag::warn_defined_in_function_type_macro);
203    else
204      PP.Diag(beginLoc, diag::warn_defined_in_object_type_macro);
205  }
206
207  // Invoke the 'defined' callback.
208  if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
209    Callbacks->Defined(macroTokenMacro,
210                       SourceRange(beginLocPeekTok.getLocation()));
211  }
212
213  // Success, remember that we saw defined(X).
214  DT.State = DefinedTracker::DefinedMacro;
215  DT.TheMacro = II;
216  return false;
217}
218
219/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
220/// return the computed value in Result.  Return true if there was an error
221/// parsing.  This function also returns information about the form of the
222/// expression in DT.  See above for information on what DT means.
223///
224/// If ValueLive is false, then this value is being evaluated in a context where
225/// the result is not used.  As such, avoid diagnostics that relate to
226/// evaluation.
227static bool EvaluateValue(PPValue &ResultToken &PeekTokDefinedTracker &DT,
228                          bool ValueLivePreprocessor &PP) {
229  DT.State = DefinedTracker::Unknown;
230
231  Result.setIdentifier(nullptr);
232
233  if (PeekTok.is(tok::code_completion)) {
234    if (PP.getCodeCompletionHandler())
235      PP.getCodeCompletionHandler()->CodeCompletePreprocessorExpression();
236    PP.setCodeCompletionReached();
237    PP.LexNonComment(PeekTok);
238  }
239
240  switch (PeekTok.getKind()) {
241  default:
242    // If this token's spelling is a pp-identifier, check to see if it is
243    // 'defined' or if it is a macro.  Note that we check here because many
244    // keywords are pp-identifiers, so we can't check the kind.
245    if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
246      // Handle "defined X" and "defined(X)".
247      if (II->isStr("defined"))
248        return EvaluateDefined(ResultPeekTokDTValueLivePP);
249
250      if (!II->isCPlusPlusOperatorKeyword()) {
251        // If this identifier isn't 'defined' or one of the special
252        // preprocessor keywords and it wasn't macro expanded, it turns
253        // into a simple 0
254        if (ValueLive)
255          PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
256        Result.Val = 0;
257        Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
258        Result.setIdentifier(II);
259        Result.setRange(PeekTok.getLocation());
260        DT.IncludedUndefinedIds = true;
261        PP.LexNonComment(PeekTok);
262        return false;
263      }
264    }
265    PP.Diag(PeekTok, diag::err_pp_expr_bad_token_start_expr);
266    return true;
267  case tok::eod:
268  case tok::r_paren:
269    // If there is no expression, report and exit.
270    PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
271    return true;
272  case tok::numeric_constant: {
273    SmallString<64IntegerBuffer;
274    bool NumberInvalid = false;
275    StringRef Spelling = PP.getSpelling(PeekTok, IntegerBuffer,
276                                              &NumberInvalid);
277    if (NumberInvalid)
278      return true// a diagnostic was already reported
279
280    NumericLiteralParser Literal(Spelling, PeekTok.getLocation(), PP);
281    if (Literal.hadError)
282      return true// a diagnostic was already reported.
283
284    if (Literal.isFloatingLiteral() || Literal.isImaginary) {
285      PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
286      return true;
287    }
288     (0) . __assert_fail ("Literal.isIntegerLiteral() && \"Unknown ppnumber\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPExpressions.cpp", 288, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
289
290    // Complain about, and drop, any ud-suffix.
291    if (Literal.hasUDSuffix())
292      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*integer*/1;
293
294    // 'long long' is a C99 or C++11 feature.
295    if (!PP.getLangOpts().C99 && Literal.isLongLong) {
296      if (PP.getLangOpts().CPlusPlus)
297        PP.Diag(PeekTok,
298             PP.getLangOpts().CPlusPlus11 ?
299             diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong);
300      else
301        PP.Diag(PeekTok, diag::ext_c99_longlong);
302    }
303
304    // Parse the integer literal into Result.
305    if (Literal.GetIntegerValue(Result.Val)) {
306      // Overflow parsing integer literal.
307      if (ValueLive)
308        PP.Diag(PeekTok, diag::err_integer_literal_too_large)
309            << /* Unsigned */ 1;
310      Result.Val.setIsUnsigned(true);
311    } else {
312      // Set the signedness of the result to match whether there was a U suffix
313      // or not.
314      Result.Val.setIsUnsigned(Literal.isUnsigned);
315
316      // Detect overflow based on whether the value is signed.  If signed
317      // and if the value is too large, emit a warning "integer constant is so
318      // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
319      // is 64-bits.
320      if (!Literal.isUnsigned && Result.Val.isNegative()) {
321        // Octal, hexadecimal, and binary literals are implicitly unsigned if
322        // the value does not fit into a signed integer type.
323        if (ValueLive && Literal.getRadix() == 10)
324          PP.Diag(PeekTok, diag::ext_integer_literal_too_large_for_signed);
325        Result.Val.setIsUnsigned(true);
326      }
327    }
328
329    // Consume the token.
330    Result.setRange(PeekTok.getLocation());
331    PP.LexNonComment(PeekTok);
332    return false;
333  }
334  case tok::char_constant:          // 'x'
335  case tok::wide_char_constant:     // L'x'
336  case tok::utf8_char_constant:     // u8'x'
337  case tok::utf16_char_constant:    // u'x'
338  case tok::utf32_char_constant: {  // U'x'
339    // Complain about, and drop, any ud-suffix.
340    if (PeekTok.hasUDSuffix())
341      PP.Diag(PeekTok, diag::err_pp_invalid_udl) << /*character*/0;
342
343    SmallString<32CharBuffer;
344    bool CharInvalid = false;
345    StringRef ThisTok = PP.getSpelling(PeekTok, CharBuffer, &CharInvalid);
346    if (CharInvalid)
347      return true;
348
349    CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(),
350                              PeekTok.getLocation(), PP, PeekTok.getKind());
351    if (Literal.hadError())
352      return true;  // A diagnostic was already emitted.
353
354    // Character literals are always int or wchar_t, expand to intmax_t.
355    const TargetInfo &TI = PP.getTargetInfo();
356    unsigned NumBits;
357    if (Literal.isMultiChar())
358      NumBits = TI.getIntWidth();
359    else if (Literal.isWide())
360      NumBits = TI.getWCharWidth();
361    else if (Literal.isUTF16())
362      NumBits = TI.getChar16Width();
363    else if (Literal.isUTF32())
364      NumBits = TI.getChar32Width();
365    else // char or char8_t
366      NumBits = TI.getCharWidth();
367
368    // Set the width.
369    llvm::APSInt Val(NumBits);
370    // Set the value.
371    Val = Literal.getValue();
372    // Set the signedness. UTF-16 and UTF-32 are always unsigned
373    if (Literal.isWide())
374      Val.setIsUnsigned(!TargetInfo::isTypeSigned(TI.getWCharType()));
375    else if (!Literal.isUTF16() && !Literal.isUTF32())
376      Val.setIsUnsigned(!PP.getLangOpts().CharIsSigned);
377
378    if (Result.Val.getBitWidth() > Val.getBitWidth()) {
379      Result.Val = Val.extend(Result.Val.getBitWidth());
380    } else {
381       (0) . __assert_fail ("Result.Val.getBitWidth() == Val.getBitWidth() && \"intmax_t smaller than char/wchar_t?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPExpressions.cpp", 382, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Result.Val.getBitWidth() == Val.getBitWidth() &&
382 (0) . __assert_fail ("Result.Val.getBitWidth() == Val.getBitWidth() && \"intmax_t smaller than char/wchar_t?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPExpressions.cpp", 382, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">             "intmax_t smaller than char/wchar_t?");
383      Result.Val = Val;
384    }
385
386    // Consume the token.
387    Result.setRange(PeekTok.getLocation());
388    PP.LexNonComment(PeekTok);
389    return false;
390  }
391  case tok::l_paren: {
392    SourceLocation Start = PeekTok.getLocation();
393    PP.LexNonComment(PeekTok);  // Eat the (.
394    // Parse the value and if there are any binary operators involved, parse
395    // them.
396    if (EvaluateValue(ResultPeekTokDTValueLivePP)) return true;
397
398    // If this is a silly value like (X), which doesn't need parens, check for
399    // !(defined X).
400    if (PeekTok.is(tok::r_paren)) {
401      // Just use DT unmodified as our result.
402    } else {
403      // Otherwise, we have something like (x+y), and we consumed '(x'.
404      if (EvaluateDirectiveSubExpr(Result1PeekTokValueLive,
405                                   DT.IncludedUndefinedIdsPP))
406        return true;
407
408      if (PeekTok.isNot(tok::r_paren)) {
409        PP.Diag(PeekTok.getLocation(), diag::err_pp_expected_rparen)
410          << Result.getRange();
411        PP.Diag(Start, diag::note_matching) << tok::l_paren;
412        return true;
413      }
414      DT.State = DefinedTracker::Unknown;
415    }
416    Result.setRange(StartPeekTok.getLocation());
417    Result.setIdentifier(nullptr);
418    PP.LexNonComment(PeekTok);  // Eat the ).
419    return false;
420  }
421  case tok::plus: {
422    SourceLocation Start = PeekTok.getLocation();
423    // Unary plus doesn't modify the value.
424    PP.LexNonComment(PeekTok);
425    if (EvaluateValue(ResultPeekTokDTValueLivePP)) return true;
426    Result.setBegin(Start);
427    Result.setIdentifier(nullptr);
428    return false;
429  }
430  case tok::minus: {
431    SourceLocation Loc = PeekTok.getLocation();
432    PP.LexNonComment(PeekTok);
433    if (EvaluateValue(ResultPeekTokDTValueLivePP)) return true;
434    Result.setBegin(Loc);
435    Result.setIdentifier(nullptr);
436
437    // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
438    Result.Val = -Result.Val;
439
440    // -MININT is the only thing that overflows.  Unsigned never overflows.
441    bool Overflow = !Result.isUnsigned() && Result.Val.isMinSignedValue();
442
443    // If this operator is live and overflowed, report the issue.
444    if (Overflow && ValueLive)
445      PP.Diag(Loc, diag::warn_pp_expr_overflow) << Result.getRange();
446
447    DT.State = DefinedTracker::Unknown;
448    return false;
449  }
450
451  case tok::tilde: {
452    SourceLocation Start = PeekTok.getLocation();
453    PP.LexNonComment(PeekTok);
454    if (EvaluateValue(ResultPeekTokDTValueLivePP)) return true;
455    Result.setBegin(Start);
456    Result.setIdentifier(nullptr);
457
458    // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
459    Result.Val = ~Result.Val;
460    DT.State = DefinedTracker::Unknown;
461    return false;
462  }
463
464  case tok::exclaim: {
465    SourceLocation Start = PeekTok.getLocation();
466    PP.LexNonComment(PeekTok);
467    if (EvaluateValue(ResultPeekTokDTValueLivePP)) return true;
468    Result.setBegin(Start);
469    Result.Val = !Result.Val;
470    // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
471    Result.Val.setIsUnsigned(false);
472    Result.setIdentifier(nullptr);
473
474    if (DT.State == DefinedTracker::DefinedMacro)
475      DT.State = DefinedTracker::NotDefinedMacro;
476    else if (DT.State == DefinedTracker::NotDefinedMacro)
477      DT.State = DefinedTracker::DefinedMacro;
478    return false;
479  }
480  case tok::kw_true:
481  case tok::kw_false:
482    Result.Val = PeekTok.getKind() == tok::kw_true;
483    Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
484    Result.setIdentifier(PeekTok.getIdentifierInfo());
485    Result.setRange(PeekTok.getLocation());
486    PP.LexNonComment(PeekTok);
487    return false;
488
489  // FIXME: Handle #assert
490  }
491}
492
493/// getPrecedence - Return the precedence of the specified binary operator
494/// token.  This returns:
495///   ~0 - Invalid token.
496///   14 -> 3 - various operators.
497///    0 - 'eod' or ')'
498static unsigned getPrecedence(tok::TokenKind Kind) {
499  switch (Kind) {
500  defaultreturn ~0U;
501  case tok::percent:
502  case tok::slash:
503  case tok::star:                 return 14;
504  case tok::plus:
505  case tok::minus:                return 13;
506  case tok::lessless:
507  case tok::greatergreater:       return 12;
508  case tok::lessequal:
509  case tok::less:
510  case tok::greaterequal:
511  case tok::greater:              return 11;
512  case tok::exclaimequal:
513  case tok::equalequal:           return 10;
514  case tok::amp:                  return 9;
515  case tok::caret:                return 8;
516  case tok::pipe:                 return 7;
517  case tok::ampamp:               return 6;
518  case tok::pipepipe:             return 5;
519  case tok::question:             return 4;
520  case tok::comma:                return 3;
521  case tok::colon:                return 2;
522  case tok::r_paren:              return 0;// Lowest priority, end of expr.
523  case tok::eod:                  return 0;// Lowest priority, end of directive.
524  }
525}
526
527static void diagnoseUnexpectedOperator(Preprocessor &PPPPValue &LHS,
528                                       Token &Tok) {
529  if (Tok.is(tok::l_paren) && LHS.getIdentifier())
530    PP.Diag(LHS.getRange().getBegin(), diag::err_pp_expr_bad_token_lparen)
531        << LHS.getIdentifier();
532  else
533    PP.Diag(Tok.getLocation(), diag::err_pp_expr_bad_token_binop)
534        << LHS.getRange();
535}
536
537/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
538/// PeekTok, and whose precedence is PeekPrec.  This returns the result in LHS.
539///
540/// If ValueLive is false, then this value is being evaluated in a context where
541/// the result is not used.  As such, avoid diagnostics that relate to
542/// evaluation, such as division by zero warnings.
543static bool EvaluateDirectiveSubExpr(PPValue &LHSunsigned MinPrec,
544                                     Token &PeekTokbool ValueLive,
545                                     bool &IncludedUndefinedIds,
546                                     Preprocessor &PP) {
547  unsigned PeekPrec = getPrecedence(PeekTok.getKind());
548  // If this token isn't valid, report the error.
549  if (PeekPrec == ~0U) {
550    diagnoseUnexpectedOperator(PPLHSPeekTok);
551    return true;
552  }
553
554  while (true) {
555    // If this token has a lower precedence than we are allowed to parse, return
556    // it so that higher levels of the recursion can parse it.
557    if (PeekPrec < MinPrec)
558      return false;
559
560    tok::TokenKind Operator = PeekTok.getKind();
561
562    // If this is a short-circuiting operator, see if the RHS of the operator is
563    // dead.  Note that this cannot just clobber ValueLive.  Consider
564    // "0 && 1 ? 4 : 1 / 0", which is parsed as "(0 && 1) ? 4 : (1 / 0)".  In
565    // this example, the RHS of the && being dead does not make the rest of the
566    // expr dead.
567    bool RHSIsLive;
568    if (Operator == tok::ampamp && LHS.Val == 0)
569      RHSIsLive = false;   // RHS of "0 && x" is dead.
570    else if (Operator == tok::pipepipe && LHS.Val != 0)
571      RHSIsLive = false;   // RHS of "1 || x" is dead.
572    else if (Operator == tok::question && LHS.Val == 0)
573      RHSIsLive = false;   // RHS (x) of "0 ? x : y" is dead.
574    else
575      RHSIsLive = ValueLive;
576
577    // Consume the operator, remembering the operator's location for reporting.
578    SourceLocation OpLoc = PeekTok.getLocation();
579    PP.LexNonComment(PeekTok);
580
581    PPValue RHS(LHS.getBitWidth());
582    // Parse the RHS of the operator.
583    DefinedTracker DT;
584    if (EvaluateValue(RHSPeekTokDTRHSIsLivePP)) return true;
585    IncludedUndefinedIds = DT.IncludedUndefinedIds;
586
587    // Remember the precedence of this operator and get the precedence of the
588    // operator immediately to the right of the RHS.
589    unsigned ThisPrec = PeekPrec;
590    PeekPrec = getPrecedence(PeekTok.getKind());
591
592    // If this token isn't valid, report the error.
593    if (PeekPrec == ~0U) {
594      diagnoseUnexpectedOperator(PPRHSPeekTok);
595      return true;
596    }
597
598    // Decide whether to include the next binop in this subexpression.  For
599    // example, when parsing x+y*z and looking at '*', we want to recursively
600    // handle y*z as a single subexpression.  We do this because the precedence
601    // of * is higher than that of +.  The only strange case we have to handle
602    // here is for the ?: operator, where the precedence is actually lower than
603    // the LHS of the '?'.  The grammar rule is:
604    //
605    // conditional-expression ::=
606    //    logical-OR-expression ? expression : conditional-expression
607    // where 'expression' is actually comma-expression.
608    unsigned RHSPrec;
609    if (Operator == tok::question)
610      // The RHS of "?" should be maximally consumed as an expression.
611      RHSPrec = getPrecedence(tok::comma);
612    else  // All others should munch while higher precedence.
613      RHSPrec = ThisPrec+1;
614
615    if (PeekPrec >= RHSPrec) {
616      if (EvaluateDirectiveSubExpr(RHSRHSPrecPeekTokRHSIsLive,
617                                   IncludedUndefinedIdsPP))
618        return true;
619      PeekPrec = getPrecedence(PeekTok.getKind());
620    }
621     (0) . __assert_fail ("PeekPrec <= ThisPrec && \"Recursion didn't work!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPExpressions.cpp", 621, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
622
623    // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
624    // either operand is unsigned.
625    llvm::APSInt Res(LHS.getBitWidth());
626    switch (Operator) {
627    case tok::question:       // No UAC for x and y in "x ? y : z".
628    case tok::lessless:       // Shift amount doesn't UAC with shift value.
629    case tok::greatergreater// Shift amount doesn't UAC with shift value.
630    case tok::comma:          // Comma operands are not subject to UACs.
631    case tok::pipepipe:       // Logical || does not do UACs.
632    case tok::ampamp:         // Logical && does not do UACs.
633      break;                  // No UAC
634    default:
635      Res.setIsUnsigned(LHS.isUnsigned()|RHS.isUnsigned());
636      // If this just promoted something from signed to unsigned, and if the
637      // value was negative, warn about it.
638      if (ValueLive && Res.isUnsigned()) {
639        if (!LHS.isUnsigned() && LHS.Val.isNegative())
640          PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 0
641            << LHS.Val.toString(10true) + " to " +
642               LHS.Val.toString(10false)
643            << LHS.getRange() << RHS.getRange();
644        if (!RHS.isUnsigned() && RHS.Val.isNegative())
645          PP.Diag(OpLoc, diag::warn_pp_convert_to_positive) << 1
646            << RHS.Val.toString(10true) + " to " +
647               RHS.Val.toString(10false)
648            << LHS.getRange() << RHS.getRange();
649      }
650      LHS.Val.setIsUnsigned(Res.isUnsigned());
651      RHS.Val.setIsUnsigned(Res.isUnsigned());
652    }
653
654    bool Overflow = false;
655    switch (Operator) {
656    default: llvm_unreachable("Unknown operator token!");
657    case tok::percent:
658      if (RHS.Val != 0)
659        Res = LHS.Val % RHS.Val;
660      else if (ValueLive) {
661        PP.Diag(OpLoc, diag::err_pp_remainder_by_zero)
662          << LHS.getRange() << RHS.getRange();
663        return true;
664      }
665      break;
666    case tok::slash:
667      if (RHS.Val != 0) {
668        if (LHS.Val.isSigned())
669          Res = llvm::APSInt(LHS.Val.sdiv_ov(RHS.Val, Overflow), false);
670        else
671          Res = LHS.Val / RHS.Val;
672      } else if (ValueLive) {
673        PP.Diag(OpLoc, diag::err_pp_division_by_zero)
674          << LHS.getRange() << RHS.getRange();
675        return true;
676      }
677      break;
678
679    case tok::star:
680      if (Res.isSigned())
681        Res = llvm::APSInt(LHS.Val.smul_ov(RHS.Val, Overflow), false);
682      else
683        Res = LHS.Val * RHS.Val;
684      break;
685    case tok::lessless: {
686      // Determine whether overflow is about to happen.
687      if (LHS.isUnsigned())
688        Res = LHS.Val.ushl_ov(RHS.Val, Overflow);
689      else
690        Res = llvm::APSInt(LHS.Val.sshl_ov(RHS.Val, Overflow), false);
691      break;
692    }
693    case tok::greatergreater: {
694      // Determine whether overflow is about to happen.
695      unsigned ShAmt = static_cast<unsigned>(RHS.Val.getLimitedValue());
696      if (ShAmt >= LHS.getBitWidth()) {
697        Overflow = true;
698        ShAmt = LHS.getBitWidth()-1;
699      }
700      Res = LHS.Val >> ShAmt;
701      break;
702    }
703    case tok::plus:
704      if (LHS.isUnsigned())
705        Res = LHS.Val + RHS.Val;
706      else
707        Res = llvm::APSInt(LHS.Val.sadd_ov(RHS.Val, Overflow), false);
708      break;
709    case tok::minus:
710      if (LHS.isUnsigned())
711        Res = LHS.Val - RHS.Val;
712      else
713        Res = llvm::APSInt(LHS.Val.ssub_ov(RHS.Val, Overflow), false);
714      break;
715    case tok::lessequal:
716      Res = LHS.Val <= RHS.Val;
717      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
718      break;
719    case tok::less:
720      Res = LHS.Val < RHS.Val;
721      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
722      break;
723    case tok::greaterequal:
724      Res = LHS.Val >= RHS.Val;
725      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
726      break;
727    case tok::greater:
728      Res = LHS.Val > RHS.Val;
729      Res.setIsUnsigned(false);  // C99 6.5.8p6, result is always int (signed)
730      break;
731    case tok::exclaimequal:
732      Res = LHS.Val != RHS.Val;
733      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
734      break;
735    case tok::equalequal:
736      Res = LHS.Val == RHS.Val;
737      Res.setIsUnsigned(false);  // C99 6.5.9p3, result is always int (signed)
738      break;
739    case tok::amp:
740      Res = LHS.Val & RHS.Val;
741      break;
742    case tok::caret:
743      Res = LHS.Val ^ RHS.Val;
744      break;
745    case tok::pipe:
746      Res = LHS.Val | RHS.Val;
747      break;
748    case tok::ampamp:
749      Res = (LHS.Val != 0 && RHS.Val != 0);
750      Res.setIsUnsigned(false);  // C99 6.5.13p3, result is always int (signed)
751      break;
752    case tok::pipepipe:
753      Res = (LHS.Val != 0 || RHS.Val != 0);
754      Res.setIsUnsigned(false);  // C99 6.5.14p3, result is always int (signed)
755      break;
756    case tok::comma:
757      // Comma is invalid in pp expressions in c89/c++ mode, but is valid in C99
758      // if not being evaluated.
759      if (!PP.getLangOpts().C99 || ValueLive)
760        PP.Diag(OpLoc, diag::ext_pp_comma_expr)
761          << LHS.getRange() << RHS.getRange();
762      Res = RHS.Val; // LHS = LHS,RHS -> RHS.
763      break;
764    case tok::question: {
765      // Parse the : part of the expression.
766      if (PeekTok.isNot(tok::colon)) {
767        PP.Diag(PeekTok.getLocation(), diag::err_expected)
768            << tok::colon << LHS.getRange() << RHS.getRange();
769        PP.Diag(OpLoc, diag::note_matching) << tok::question;
770        return true;
771      }
772      // Consume the :.
773      PP.LexNonComment(PeekTok);
774
775      // Evaluate the value after the :.
776      bool AfterColonLive = ValueLive && LHS.Val == 0;
777      PPValue AfterColonVal(LHS.getBitWidth());
778      DefinedTracker DT;
779      if (EvaluateValue(AfterColonValPeekTokDTAfterColonLivePP))
780        return true;
781
782      // Parse anything after the : with the same precedence as ?.  We allow
783      // things of equal precedence because ?: is right associative.
784      if (EvaluateDirectiveSubExpr(AfterColonValThisPrec,
785                                   PeekTokAfterColonLive,
786                                   IncludedUndefinedIdsPP))
787        return true;
788
789      // Now that we have the condition, the LHS and the RHS of the :, evaluate.
790      Res = LHS.Val != 0 ? RHS.Val : AfterColonVal.Val;
791      RHS.setEnd(AfterColonVal.getRange().getEnd());
792
793      // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
794      // either operand is unsigned.
795      Res.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
796
797      // Figure out the precedence of the token after the : part.
798      PeekPrec = getPrecedence(PeekTok.getKind());
799      break;
800    }
801    case tok::colon:
802      // Don't allow :'s to float around without being part of ?: exprs.
803      PP.Diag(OpLoc, diag::err_pp_colon_without_question)
804        << LHS.getRange() << RHS.getRange();
805      return true;
806    }
807
808    // If this operator is live and overflowed, report the issue.
809    if (Overflow && ValueLive)
810      PP.Diag(OpLoc, diag::warn_pp_expr_overflow)
811        << LHS.getRange() << RHS.getRange();
812
813    // Put the result back into 'LHS' for our next iteration.
814    LHS.Val = Res;
815    LHS.setEnd(RHS.getRange().getEnd());
816    RHS.setIdentifier(nullptr);
817  }
818}
819
820/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
821/// may occur after a #if or #elif directive.  If the expression is equivalent
822/// to "!defined(X)" return X in IfNDefMacro.
823Preprocessor::DirectiveEvalResult
824Preprocessor::EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
825  SaveAndRestore<boolPPDir(ParsingIfOrElifDirective, true);
826  // Save the current state of 'DisableMacroExpansion' and reset it to false. If
827  // 'DisableMacroExpansion' is true, then we must be in a macro argument list
828  // in which case a directive is undefined behavior.  We want macros to be able
829  // to recursively expand in order to get more gcc-list behavior, so we force
830  // DisableMacroExpansion to false and restore it when we're done parsing the
831  // expression.
832  bool DisableMacroExpansionAtStartOfDirective = DisableMacroExpansion;
833  DisableMacroExpansion = false;
834
835  // Peek ahead one token.
836  Token Tok;
837  LexNonComment(Tok);
838
839  // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
840  unsigned BitWidth = getTargetInfo().getIntMaxTWidth();
841
842  PPValue ResVal(BitWidth);
843  DefinedTracker DT;
844  SourceLocation ExprStartLoc = SourceMgr.getExpansionLoc(Tok.getLocation());
845  if (EvaluateValue(ResValTokDTtrue*this)) {
846    // Parse error, skip the rest of the macro line.
847    SourceRange ConditionRange = ExprStartLoc;
848    if (Tok.isNot(tok::eod))
849      ConditionRange = DiscardUntilEndOfDirective();
850
851    // Restore 'DisableMacroExpansion'.
852    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
853
854    // We cannot trust the source range from the value because there was a
855    // parse error. Track the range manually -- the end of the directive is the
856    // end of the condition range.
857    return {false,
858            DT.IncludedUndefinedIds,
859            {ExprStartLocConditionRange.getEnd()}};
860  }
861
862  // If we are at the end of the expression after just parsing a value, there
863  // must be no (unparenthesized) binary operators involved, so we can exit
864  // directly.
865  if (Tok.is(tok::eod)) {
866    // If the expression we parsed was of the form !defined(macro), return the
867    // macro in IfNDefMacro.
868    if (DT.State == DefinedTracker::NotDefinedMacro)
869      IfNDefMacro = DT.TheMacro;
870
871    // Restore 'DisableMacroExpansion'.
872    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
873    return {ResVal.Val != 0DT.IncludedUndefinedIdsResVal.getRange()};
874  }
875
876  // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
877  // operator and the stuff after it.
878  if (EvaluateDirectiveSubExpr(ResValgetPrecedence(tok::question),
879                               ToktrueDT.IncludedUndefinedIds*this)) {
880    // Parse error, skip the rest of the macro line.
881    if (Tok.isNot(tok::eod))
882      DiscardUntilEndOfDirective();
883
884    // Restore 'DisableMacroExpansion'.
885    DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
886    return {falseDT.IncludedUndefinedIdsResVal.getRange()};
887  }
888
889  // If we aren't at the tok::eod token, something bad happened, like an extra
890  // ')' token.
891  if (Tok.isNot(tok::eod)) {
892    Diag(Tok, diag::err_pp_expected_eol);
893    DiscardUntilEndOfDirective();
894  }
895
896  // Restore 'DisableMacroExpansion'.
897  DisableMacroExpansion = DisableMacroExpansionAtStartOfDirective;
898  return {ResVal.Val != 0DT.IncludedUndefinedIdsResVal.getRange()};
899}
900
DefinedTracker::TrackerState
clang::Preprocessor::EvaluateDirectiveExpression