[clang] 32805e6 - [clang] Dont print implicit forrange initializer

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 17 02:30:14 PDT 2022


Author: Kadir Cetinkaya
Date: 2022-06-17T11:29:44+02:00
New Revision: 32805e60c9de1f82887cd2af30d247dcabd2e1d3

URL: https://github.com/llvm/llvm-project/commit/32805e60c9de1f82887cd2af30d247dcabd2e1d3
DIFF: https://github.com/llvm/llvm-project/commit/32805e60c9de1f82887cd2af30d247dcabd2e1d3.diff

LOG: [clang] Dont print implicit forrange initializer

Fixes https://github.com/clangd/clangd/issues/1158

Differential Revision: https://reviews.llvm.org/D127863

Added: 
    

Modified: 
    clang/lib/AST/DeclPrinter.cpp
    clang/unittests/AST/DeclPrinterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index faafe307f03cf..c6a392c9c01b5 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -895,12 +895,15 @@ void DeclPrinter::VisitVarDecl(VarDecl *D) {
   Expr *Init = D->getInit();
   if (!Policy.SuppressInitializers && Init) {
     bool ImplicitInit = false;
-    if (CXXConstructExpr *Construct =
-            dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
+    if (D->isCXXForRangeDecl()) {
+      // FIXME: We should print the range expression instead.
+      ImplicitInit = true;
+    } else if (CXXConstructExpr *Construct =
+                   dyn_cast<CXXConstructExpr>(Init->IgnoreImplicit())) {
       if (D->getInitStyle() == VarDecl::CallInit &&
           !Construct->isListInitialization()) {
         ImplicitInit = Construct->getNumArgs() == 0 ||
-          Construct->getArg(0)->isDefaultArgument();
+                       Construct->getArg(0)->isDefaultArgument();
       }
     }
     if (!ImplicitInit) {

diff  --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp
index c2d7d78738f96..11dca6ed68167 100644
--- a/clang/unittests/AST/DeclPrinterTest.cpp
+++ b/clang/unittests/AST/DeclPrinterTest.cpp
@@ -1426,4 +1426,7 @@ TEST(DeclPrinter, VarDeclWithInitializer) {
   ASSERT_TRUE(PrintedDeclCXX17Matches(
       "int a = 0x15;", namedDecl(hasName("a")).bind("id"), "int a = 0x15",
       [](PrintingPolicy &Policy) { Policy.ConstantsAsWritten = true; }));
+  ASSERT_TRUE(
+      PrintedDeclCXX17Matches("void foo() {int arr[42]; for(int a : arr);}",
+                              namedDecl(hasName("a")).bind("id"), "int a"));
 }


        


More information about the cfe-commits mailing list