r317032 - Fix usage of right shift operator in fold expressions

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 31 13:29:23 PDT 2017


Author: rsmith
Date: Tue Oct 31 13:29:22 2017
New Revision: 317032

URL: http://llvm.org/viewvc/llvm-project?rev=317032&view=rev
Log:
Fix usage of right shift operator in fold expressions

The right shift operator was not seen as a valid operator in a fold expression, which is PR32563.

Patch by Nicolas Lesser ("Blitz Rakete")!

Modified:
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/ParseExpr.cpp
    cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=317032&r1=317031&r2=317032&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Tue Oct 31 13:29:22 2017
@@ -506,6 +506,12 @@ private:
            Kind == tok::annot_module_end || Kind == tok::annot_module_include;
   }
 
+  /// \brief Checks if the \p Level is valid for use in a fold expression.
+  bool isFoldOperator(prec::Level Level) const;
+
+  /// \brief Checks if the \p Kind is a valid operator for fold expressions.
+  bool isFoldOperator(tok::TokenKind Kind) const;
+
   /// \brief Initialize all pragma handlers.
   void initializePragmaHandlers();
 

Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=317032&r1=317031&r2=317032&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Tue Oct 31 13:29:22 2017
@@ -266,11 +266,12 @@ bool Parser::diagnoseUnknownTemplateId(E
   return false;
 }
 
-static bool isFoldOperator(prec::Level Level) {
+bool Parser::isFoldOperator(prec::Level Level) const {
   return Level > prec::Unknown && Level != prec::Conditional;
 }
-static bool isFoldOperator(tok::TokenKind Kind) {
-  return isFoldOperator(getBinOpPrecedence(Kind, false, true));
+
+bool Parser::isFoldOperator(tok::TokenKind Kind) const {
+  return isFoldOperator(getBinOpPrecedence(Kind, GreaterThanIsOperator, true));
 }
 
 /// \brief Parse a binary expression that starts with \p LHS and has a

Modified: cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp?rev=317032&r1=317031&r2=317032&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp (original)
+++ cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp Tue Oct 31 13:29:22 2017
@@ -43,3 +43,20 @@ template<typename ...T> void as_operand_
     (int)(undeclared_junk + ...) + // expected-error {{undeclared}}
     (int)(a + ...); // expected-error {{does not contain any unexpanded}}
 }
+
+// fold-operator can be '>' or '>>'.
+template <int... N> constexpr bool greaterThan() { return (N > ...); }
+template <int... N> constexpr int rightShift() { return (N >> ...); }
+
+static_assert(greaterThan<2, 1>());
+static_assert(rightShift<10, 1>() == 5);
+
+template <auto V> constexpr auto Identity = V;
+
+// Support fold operators within templates.
+template <int... N> constexpr int nestedFoldOperator() {
+  return Identity<(Identity<0> >> ... >> N)> +
+    Identity<(N >> ... >> Identity<0>)>;
+}
+
+static_assert(nestedFoldOperator<3, 1>() == 1);




More information about the cfe-commits mailing list