[PATCH] D112334: [clang-tidy] Suppress readability-static-access-through-instance for CUDA built-in variables

Carlos Galvez via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 22 10:42:55 PDT 2021


carlosgalvezp created this revision.
carlosgalvezp added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, yaxunl.
carlosgalvezp requested review of this revision.

clang-tidy can be used to statically analyze CUDA code,
thanks to clang being able to compile CUDA code natively.
This makes clang-tidy the one and only open-source
static analyzer for CUDA.

However it currently warns for native CUDA built-in
variables, like threadIdx, due to the way they
are implemented in clang.

Users don't need to know the details of the clang
implementation, and they should continue to write
idiomatic code. Therefore, suppress the warning
if a CUDA built-in variable is encountered.

Fixes https://bugs.llvm.org/show_bug.cgi?id=48758


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112334

Files:
  clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp
@@ -248,3 +248,38 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
   // CHECK-FIXES: {{^}}  Outer::S::I;{{$}}
 }
+
+namespace Bugzilla_48758 {
+
+// From __clang_cuda_builtin_vars.h
+struct __cuda_builtin_threadIdx_t {
+  static unsigned int x;
+};
+
+struct __cuda_builtin_blockIdx_t {
+  static unsigned int x;
+};
+
+struct __cuda_builtin_blockDim_t {
+  static unsigned int x;
+};
+
+struct __cuda_builtin_gridDim_t {
+  static unsigned int x;
+};
+
+__cuda_builtin_threadIdx_t threadIdx;
+__cuda_builtin_blockIdx_t blockIdx;
+__cuda_builtin_blockDim_t blockDim;
+__cuda_builtin_gridDim_t gridDim;
+
+unsigned int x1 = threadIdx.x;
+// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
+unsigned int x2 = blockIdx.x;
+// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
+unsigned int x3 = blockDim.x;
+// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
+unsigned int x4 = gridDim.x;
+// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
+
+} // namespace Bugzilla_48758
Index: clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
@@ -54,7 +54,7 @@
 
   const Expr *BaseExpr = MemberExpression->getBase();
 
-  // Do not warn for overlaoded -> operators.
+  // Do not warn for overloaded -> operators.
   if (isa<CXXOperatorCallExpr>(BaseExpr))
     return;
 
@@ -70,6 +70,10 @@
   std::string BaseTypeName =
       BaseType.getAsString(PrintingPolicyWithSupressedTag);
 
+  // Do not warn for CUDA built-in variables.
+  if (BaseTypeName.find("__cuda_builtin_") != std::string::npos)
+    return;
+
   SourceLocation MemberExprStartLoc = MemberExpression->getBeginLoc();
   auto Diag =
       diag(MemberExprStartLoc, "static member accessed through instance");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112334.381599.patch
Type: text/x-patch
Size: 2347 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211022/7584cd62/attachment.bin>


More information about the llvm-commits mailing list