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");
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");
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits