[cfe-commits] r38652 - in /cfe/cfe/trunk: Lex/PPExpressions.cpp include/clang/Lex/Preprocessor.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:23:32 PDT 2007


Author: sabre
Date: Wed Jul 11 11:23:32 2007
New Revision: 38652

URL: http://llvm.org/viewvc/llvm-project?rev=38652&view=rev
Log:
Change EvaluateValue/EvaluateDirectiveSubExpr to be static functions in
PPExpressions.cpp instead of methods.

Modified:
    cfe/cfe/trunk/Lex/PPExpressions.cpp
    cfe/cfe/trunk/include/clang/Lex/Preprocessor.h

Modified: cfe/cfe/trunk/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/PPExpressions.cpp?rev=38652&r1=38651&r2=38652&view=diff

==============================================================================
--- cfe/cfe/trunk/Lex/PPExpressions.cpp (original)
+++ cfe/cfe/trunk/Lex/PPExpressions.cpp Wed Jul 11 11:23:32 2007
@@ -26,53 +26,14 @@
 using namespace llvm;
 using namespace clang;
 
-/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
-/// may occur after a #if or #elif directive.  If the expression is equivalent
-/// to "!defined(X)" return X in IfNDefMacro.
-bool Preprocessor::
-EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
-  // Peek ahead one token.
-  LexerToken Tok;
-  Lex(Tok);
+static bool EvaluateDirectiveSubExpr(int &LHS, unsigned MinPrec,
+                                     LexerToken &PeekTok, Preprocessor &PP);
 
-  int ResVal = 0;
-  if (EvaluateValue(ResVal, Tok)) {
-    // Parse error, skip the rest of the macro line.
-    if (Tok.getKind() != tok::eom)
-      DiscardUntilEndOfDirective();
-    return false;
-  }
-  
-  // If we are at the end of the expression after just parsing a value, there
-  // must be no (unparenthesized) binary operators involved, so we can exit
-  // directly.
-  if (Tok.getKind() == tok::eom) {
-    return ResVal != 0;
-  }
-  
-  // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
-  // operator and the stuff after it.
-  if (EvaluateDirectiveSubExpr(ResVal, 1, Tok)) {
-    // Parse error, skip the rest of the macro line.
-    if (Tok.getKind() != tok::eom)
-      DiscardUntilEndOfDirective();
-    return false;
-  }
-  
-  // If we aren't at the tok::eom token, something bad happened, like an extra
-  // ')' token.
-  if (Tok.getKind() != tok::eom) {
-    Diag(Tok, diag::err_pp_expected_eol);
-    DiscardUntilEndOfDirective();
-  }
-  
-  return ResVal != 0;
-}
 
 /// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
 /// return the computed value in Result.  Return true if there was an error
 /// parsing.
-bool Preprocessor::EvaluateValue(int &Result, LexerToken &PeekTok) {
+static bool EvaluateValue(int &Result, LexerToken &PeekTok, Preprocessor &PP) {
   Result = 0;
   
   // If this token's spelling is a pp-identifier, check to see if it is
@@ -83,31 +44,26 @@
     // into a simple 0.
     if (strcmp(II->getName(), "defined")) {
       Result = 0;
-      Lex(PeekTok);
+      PP.Lex(PeekTok);
       return false;
     }
 
     // Handle "defined X" and "defined(X)".
-    assert(!DisableMacroExpansion &&
-           "How could macro exp already be disabled?");
-    // Turn off macro expansion.
-    DisableMacroExpansion = true;
 
-    // Get the next token.
-    Lex(PeekTok);
+    // Get the next token, don't expand it.
+    PP.LexUnexpandedToken(PeekTok);
 
     // Two options, it can either be a pp-identifier or a (.
     bool InParens = false;
     if (PeekTok.getKind() == tok::l_paren) {
       // Found a paren, remember we saw it and skip it.
       InParens = true;
-      Lex(PeekTok);
+      PP.LexUnexpandedToken(PeekTok);
     }
     
     // If we don't have a pp-identifier now, this is an error.
     if ((II = PeekTok.getIdentifierInfo()) == 0) {
-      DisableMacroExpansion = false;
-      Diag(PeekTok, diag::err_pp_defined_requires_identifier);
+      PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
       return true;
     }
     
@@ -118,73 +74,71 @@
     if (Result) II->getMacroInfo()->setIsUsed(true);
 
     // Consume identifier.
-    Lex(PeekTok);
+    PP.Lex(PeekTok);
 
     // If we are in parens, ensure we have a trailing ).
     if (InParens) {
       if (PeekTok.getKind() != tok::r_paren) {
-        Diag(PeekTok, diag::err_pp_missing_rparen);
+        PP.Diag(PeekTok, diag::err_pp_missing_rparen);
         return true;
       }
       // Consume the ).
-      Lex(PeekTok);
+      PP.Lex(PeekTok);
     }
-    
-    DisableMacroExpansion = false;
     return false;
   }
   
   switch (PeekTok.getKind()) {
   default:  // Non-value token.
-    Diag(PeekTok, diag::err_pp_expr_bad_token);
+    PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
     return true;
   case tok::eom:
   case tok::r_paren:
     // If there is no expression, report and exit.
-    Diag(PeekTok, diag::err_pp_expected_value_in_expr);
+    PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
     return true;
   case tok::numeric_constant: {
     // FIXME: faster.  FIXME: track signs.
-    std::string Spell = getSpelling(PeekTok);
+    std::string Spell = PP.getSpelling(PeekTok);
     // FIXME: COMPUTE integer constants CORRECTLY.
     Result = atoi(Spell.c_str());
-    Lex(PeekTok);
+    PP.Lex(PeekTok);
     return false;
   }
   case tok::l_paren:
-    Lex(PeekTok);  // Eat the (.
+    PP.Lex(PeekTok);  // Eat the (.
     // Parse the value and if there are any binary operators involved, parse
     // them.
-    if (EvaluateValue(Result, PeekTok) ||
-        EvaluateDirectiveSubExpr(Result, 1, PeekTok))
+    if (EvaluateValue(Result, PeekTok, PP) ||
+        EvaluateDirectiveSubExpr(Result, 1, PeekTok, PP))
       return true;
 
     if (PeekTok.getKind() != tok::r_paren) {
-      Diag(PeekTok, diag::err_pp_expected_rparen);
+      PP.Diag(PeekTok, diag::err_pp_expected_rparen);
       return true;
     }
-    Lex(PeekTok);  // Eat the ).
+    PP.Lex(PeekTok);  // Eat the ).
     return false;
  
   case tok::plus:
     // Unary plus doesn't modify the value.
-    Lex(PeekTok);
-    return EvaluateValue(Result, PeekTok);
+    PP.Lex(PeekTok);
+    return EvaluateValue(Result, PeekTok, PP);
   case tok::minus:
-    Lex(PeekTok);
-    if (EvaluateValue(Result, PeekTok)) return true;
+    PP.Lex(PeekTok);
+    if (EvaluateValue(Result, PeekTok, PP)) return true;
     Result = -Result;
     return false;
     
   case tok::tilde:
-    Lex(PeekTok);
-    if (EvaluateValue(Result, PeekTok)) return true;
+    PP.Lex(PeekTok);
+    if (EvaluateValue(Result, PeekTok, PP)) return true;
     Result = ~Result;
     return false;
     
   case tok::exclaim:
-    Lex(PeekTok);
-    if (EvaluateValue(Result, PeekTok)) return true;
+    PP.Lex(PeekTok);
+    if (EvaluateValue(Result, PeekTok, PP)) return true;
     Result = !Result;
     return false;
     
@@ -245,12 +199,12 @@
 
 /// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
 /// PeekTok, and whose precedence is PeekPrec.
-bool Preprocessor::EvaluateDirectiveSubExpr(int &LHS, unsigned MinPrec,
-                                            LexerToken &PeekTok) {
+static bool EvaluateDirectiveSubExpr(int &LHS, unsigned MinPrec,
+                                     LexerToken &PeekTok, Preprocessor &PP) {
   unsigned PeekPrec = getPrecedence(PeekTok.getKind());
   // If this token isn't valid, report the error.
   if (PeekPrec == ~0U) {
-    Diag(PeekTok, diag::err_pp_expr_bad_token);
+    PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
     return true;
   }
   
@@ -264,11 +218,11 @@
 
     // Consume the operator, saving the operator token for error reporting.
     LexerToken OpToken = PeekTok;
-    Lex(PeekTok);
+    PP.Lex(PeekTok);
 
     int RHS;
     // Parse the RHS of the operator.
-    if (EvaluateValue(RHS, PeekTok)) return true;
+    if (EvaluateValue(RHS, PeekTok, PP)) return true;
 
     // Remember the precedence of this operator and get the precedence of the
     // operator immediately to the right of the RHS.
@@ -277,7 +231,7 @@
 
     // If this token isn't valid, report the error.
     if (PeekPrec == ~0U) {
-      Diag(PeekTok, diag::err_pp_expr_bad_token);
+      PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
       return true;
     }
     
@@ -287,7 +241,7 @@
     // more tightly with RHS than we do, evaluate it completely first.
     if (ThisPrec < PeekPrec ||
         (ThisPrec == PeekPrec && isRightAssoc)) {
-      if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok))
+      if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, PP))
         return true;
       PeekPrec = getPrecedence(PeekTok.getKind());
     }
@@ -297,14 +251,14 @@
     default: assert(0 && "Unknown operator token!");
     case tok::percent:
       if (RHS == 0) {
-        Diag(OpToken, diag::err_pp_remainder_by_zero);
+        PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
         return true;
       }
       LHS %= RHS;
       break;
     case tok::slash:
       if (RHS == 0) {
-        Diag(OpToken, diag::err_pp_division_by_zero);
+        PP.Diag(OpToken, diag::err_pp_division_by_zero);
         return true;
       }
       LHS /= RHS;
@@ -332,25 +286,25 @@
     case tok::ampamp:          LHS = LHS && RHS; break;
     case tok::pipepipe:        LHS = LHS || RHS; break;
     case tok::comma:
-      Diag(OpToken, diag::ext_pp_comma_expr);
+      PP.Diag(OpToken, diag::ext_pp_comma_expr);
       LHS = RHS; // LHS = LHS,RHS -> RHS.
       break; 
     case tok::question: {
       // Parse the : part of the expression.
       if (PeekTok.getKind() != tok::colon) {
-        Diag(OpToken, diag::err_pp_question_without_colon);
+        PP.Diag(OpToken, diag::err_pp_question_without_colon);
         return true;
       }
       // Consume the :.
-      Lex(PeekTok);
+      PP.Lex(PeekTok);
 
       // Evaluate the value after the :.
       int AfterColonVal = 0;
-      if (EvaluateValue(AfterColonVal, PeekTok)) return true;
+      if (EvaluateValue(AfterColonVal, PeekTok, PP)) return true;
 
       // Parse anything after the : RHS that has a higher precedence than ?.
       if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
-                                   PeekTok))
+                                   PeekTok, PP))
         return true;
       
       // Now that we have the condition, the LHS and the RHS of the :, evaluate.
@@ -362,10 +316,54 @@
     }
     case tok::colon:
       // Don't allow :'s to float around without being part of ?: exprs.
-      Diag(OpToken, diag::err_pp_colon_without_question);
+      PP.Diag(OpToken, diag::err_pp_colon_without_question);
       return true;
     }
   }
   
   return false;
 }
+
+/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
+/// may occur after a #if or #elif directive.  If the expression is equivalent
+/// to "!defined(X)" return X in IfNDefMacro.
+bool Preprocessor::
+EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
+  // Peek ahead one token.
+  LexerToken Tok;
+  Lex(Tok);
+  
+  int ResVal = 0;
+  if (EvaluateValue(ResVal, Tok, *this)) {
+    // Parse error, skip the rest of the macro line.
+    if (Tok.getKind() != tok::eom)
+      DiscardUntilEndOfDirective();
+    return false;
+  }
+  
+  // If we are at the end of the expression after just parsing a value, there
+  // must be no (unparenthesized) binary operators involved, so we can exit
+  // directly.
+  if (Tok.getKind() == tok::eom) {
+    return ResVal != 0;
+  }
+  
+  // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
+  // operator and the stuff after it.
+  if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, *this)) {
+    // Parse error, skip the rest of the macro line.
+    if (Tok.getKind() != tok::eom)
+      DiscardUntilEndOfDirective();
+    return false;
+  }
+  
+  // If we aren't at the tok::eom token, something bad happened, like an extra
+  // ')' token.
+  if (Tok.getKind() != tok::eom) {
+    Diag(Tok, diag::err_pp_expected_eol);
+    DiscardUntilEndOfDirective();
+  }
+  
+  return ResVal != 0;
+}
+

Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38652&r1=38651&r2=38652&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:23:32 2007
@@ -432,11 +432,6 @@
   /// may occur after a #if or #elif directive and return it as a bool.  If the
   /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
   bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
-  /// EvaluateValue/EvaluateDirectiveSubExpr - Used to implement
-  /// EvaluateDirectiveExpression, see PPExpressions.cpp.
-  bool EvaluateValue(int &Result, LexerToken &PeekTok);
-  bool EvaluateDirectiveSubExpr(int &LHS, unsigned MinPrec,
-                                LexerToken &PeekTok);
   
   /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
   /// #pragma GCC poison/system_header/dependency and #pragma once.





More information about the cfe-commits mailing list