Skip to content

Conversation

@tahonermann
Copy link
Contributor

Static analysis identified two uses of getAs() for which the result pointer was unconditionally dereferenced. Source code inspection confirmed that the target type is assured by prior checks. This change replaces these uses of getAs() with castAs().

The first case, in clang/lib/CodeGen/Targets/AArch64.cpp, performs a cast to BuiltinType following a check for isBuiltinType().

The second case, in clang/lib/Sema/SemaTemplateVariadic.cpp, performs a cast to PackExpansionType on the result of a call to getAsType() on an object of type TemplateArgument following confirmation that isPackExpansion() returned true and that getKind() returned TemplateArgument::Type. Inspection of isPackExpansion() revealed that it only returns true when the template argument kind is TemplateArgument::Type if isa<PackExpansionType>(getAsType()) is true.

… is assured.

Static analysis identified two uses of getAs() for which the result pointer
was unconditionally dereferenced. Source code inspection confirmed that the
target type is assured by prior checks. This change replaces these uses of
getAs() with castAs().

The first case, in clang/lib/CodeGen/Targets/AArch64.cpp, performs a cast
to BuiltinType following a check for isBuiltinType().

The second case, in clang/lib/Sema/SemaTemplateVariadic.cpp, performs a cast
to PackExpansionType on the result of a call to getAsType() on an object of
type TemplateArgument following confirmation that isPackExpansion() returned
true and that getKind() returned TemplateArgument::Type. Inspection of
isPackExpansion() revealed that it only returns true when the template argument
kind is TemplateArgument::Type if isa<PackExpansionType>(getAsType()) is true.
@tahonermann tahonermann self-assigned this Mar 6, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:AArch64 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. labels Mar 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2025

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-clang

Author: Tom Honermann (tahonermann)

Changes

Static analysis identified two uses of getAs() for which the result pointer was unconditionally dereferenced. Source code inspection confirmed that the target type is assured by prior checks. This change replaces these uses of getAs() with castAs().

The first case, in clang/lib/CodeGen/Targets/AArch64.cpp, performs a cast to BuiltinType following a check for isBuiltinType().

The second case, in clang/lib/Sema/SemaTemplateVariadic.cpp, performs a cast to PackExpansionType on the result of a call to getAsType() on an object of type TemplateArgument following confirmation that isPackExpansion() returned true and that getKind() returned TemplateArgument::Type. Inspection of isPackExpansion() revealed that it only returns true when the template argument kind is TemplateArgument::Type if isa&lt;PackExpansionType&gt;(getAsType()) is true.


Full diff: https://github.com/llvm/llvm-project/pull/130188.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/Targets/AArch64.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaTemplateVariadic.cpp (+1-1)
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index d6e0e720a0941..073ca3cc82690 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -763,7 +763,7 @@ bool AArch64ABIInfo::passAsPureScalableType(
     return false;
 
   bool isPredicate;
-  switch (Ty->getAs<BuiltinType>()->getKind()) {
+  switch (Ty->castAs<BuiltinType>()->getKind()) {
 #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId)                    \
   case BuiltinType::Id:                                                        \
     isPredicate = false;                                                       \
diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index fad00f7648848..d9256dbd07d7a 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -834,7 +834,7 @@ bool Sema::CheckParameterPacksForExpansion(
 
             if (TA.getKind() == TemplateArgument::Type)
               return !TA.getAsType()
-                          ->getAs<PackExpansionType>()
+                          ->castAs<PackExpansionType>()
                           ->getNumExpansions();
 
             if (TA.getKind() == TemplateArgument::Expression)

@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2025

@llvm/pr-subscribers-clang-codegen

Author: Tom Honermann (tahonermann)

Changes

Static analysis identified two uses of getAs() for which the result pointer was unconditionally dereferenced. Source code inspection confirmed that the target type is assured by prior checks. This change replaces these uses of getAs() with castAs().

The first case, in clang/lib/CodeGen/Targets/AArch64.cpp, performs a cast to BuiltinType following a check for isBuiltinType().

The second case, in clang/lib/Sema/SemaTemplateVariadic.cpp, performs a cast to PackExpansionType on the result of a call to getAsType() on an object of type TemplateArgument following confirmation that isPackExpansion() returned true and that getKind() returned TemplateArgument::Type. Inspection of isPackExpansion() revealed that it only returns true when the template argument kind is TemplateArgument::Type if isa&lt;PackExpansionType&gt;(getAsType()) is true.


Full diff: https://github.com/llvm/llvm-project/pull/130188.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/Targets/AArch64.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaTemplateVariadic.cpp (+1-1)
diff --git a/clang/lib/CodeGen/Targets/AArch64.cpp b/clang/lib/CodeGen/Targets/AArch64.cpp
index d6e0e720a0941..073ca3cc82690 100644
--- a/clang/lib/CodeGen/Targets/AArch64.cpp
+++ b/clang/lib/CodeGen/Targets/AArch64.cpp
@@ -763,7 +763,7 @@ bool AArch64ABIInfo::passAsPureScalableType(
     return false;
 
   bool isPredicate;
-  switch (Ty->getAs<BuiltinType>()->getKind()) {
+  switch (Ty->castAs<BuiltinType>()->getKind()) {
 #define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId)                    \
   case BuiltinType::Id:                                                        \
     isPredicate = false;                                                       \
diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index fad00f7648848..d9256dbd07d7a 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -834,7 +834,7 @@ bool Sema::CheckParameterPacksForExpansion(
 
             if (TA.getKind() == TemplateArgument::Type)
               return !TA.getAsType()
-                          ->getAs<PackExpansionType>()
+                          ->castAs<PackExpansionType>()
                           ->getNumExpansions();
 
             if (TA.getKind() == TemplateArgument::Expression)

@tahonermann tahonermann merged commit 813325e into llvm:main Mar 6, 2025
12 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:AArch64 clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants