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