[clang] 1f451a8 - [clang] initialize type qualifiers for FunctionNoProtoType

Richard Howell via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 26 09:49:05 PDT 2022


Author: Richard Howell
Date: 2022-09-26T09:48:54-07:00
New Revision: 1f451a8bd6f32465b6ff26c30ba7fb6fc7e0e689

URL: https://github.com/llvm/llvm-project/commit/1f451a8bd6f32465b6ff26c30ba7fb6fc7e0e689
DIFF: https://github.com/llvm/llvm-project/commit/1f451a8bd6f32465b6ff26c30ba7fb6fc7e0e689.diff

LOG: [clang] initialize type qualifiers for FunctionNoProtoType

When initializing FunctionNoProtoType types, zero out the type
qualifiers. This will ensure the ODR hash remains stable as it
hashes the values for these qualifiers for all function types.

Reviewed By: aaron.ballman

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

Added: 
    

Modified: 
    clang/include/clang/AST/Type.h
    clang/unittests/AST/DeclTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 347088780e4b8..507f72e62988e 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -3892,7 +3892,10 @@ class FunctionType : public Type {
   }
 
   Qualifiers getFastTypeQuals() const {
-    return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
+    if (isFunctionProtoType())
+      return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals);
+
+    return Qualifiers();
   }
 
 public:

diff  --git a/clang/unittests/AST/DeclTest.cpp b/clang/unittests/AST/DeclTest.cpp
index 43a3798867f49..8d55f13e1f5c3 100644
--- a/clang/unittests/AST/DeclTest.cpp
+++ b/clang/unittests/AST/DeclTest.cpp
@@ -354,3 +354,25 @@ TEST(Decl, FriendFunctionWithinClassInHeaderUnit) {
 
   EXPECT_TRUE(getFooValue->isInlined());
 }
+
+TEST(Decl, NoProtoFunctionDeclAttributes) {
+  llvm::Annotations Code(R"(
+    void f();
+    )");
+
+  auto AST = tooling::buildASTFromCodeWithArgs(
+      Code.code(),
+      /*Args=*/{"-target", "i386-apple-darwin", "-x", "objective-c",
+                "-std=c89"});
+  ASTContext &Ctx = AST->getASTContext();
+
+  auto *f = selectFirst<FunctionDecl>(
+      "f", match(functionDecl(hasName("f")).bind("f"), Ctx));
+
+  const auto *FPT = f->getType()->getAs<FunctionNoProtoType>();
+
+  // Functions without prototypes always have 0 initialized qualifiers
+  EXPECT_FALSE(FPT->isConst());
+  EXPECT_FALSE(FPT->isVolatile());
+  EXPECT_FALSE(FPT->isRestrict());
+}


        


More information about the cfe-commits mailing list