Skip to content

Commit 4eedd51

Browse files
earnolVladislav Aranov
andauthored
[clang-tidy] Do not provide diagnostics for cert-dcl58-cpp on implicit declarations (llvm#188152)
Do not provide diagnostics for cert-dcl58-cpp for compiler generated intrinsic as it will be a false positive. In provided tests compiler generates align_val_t which ends up inside std namespace, resulting in std::align_val_t symbol. This symbol is compiler generated, having no location, causing compiler crash. Also there is no point to notify user about violations which user has no control of. Resolution: Diagnostics suppressed. Co-authored-by: Vladislav Aranov <vladislav.aranov@ericsson.com>
1 parent de6ed3c commit 4eedd51

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ void clang::tidy::bugprone::StdNamespaceModificationCheck::check(
117117
if (!D || !NS)
118118
return;
119119

120+
// Skip compiler-generated implicit declarations (e.g. std::align_val_t).
121+
if (D->isImplicit())
122+
return;
123+
120124
diag(D->getLocation(),
121125
"modification of %0 namespace can result in undefined behavior")
122126
<< NS;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ Changes in existing checks
230230
- Improved :doc:`bugprone-std-namespace-modification
231231
<clang-tidy/checks/bugprone/std-namespace-modification>` check by fixing
232232
false positives when extending the standard library with a specialization of
233-
user-defined type.
233+
user-defined type and by removing detection of the compiler generated ``std``
234+
namespace extensions.
234235

235236
- Improved :doc:`bugprone-string-constructor
236237
<clang-tidy/checks/bugprone/string-constructor>` check to detect suspicious
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-std-namespace-modification %t \
2+
// RUN: -- -system-headers
3+
4+
// Compiler-generated implicit declarations in namespace std (e.g.
5+
// std::align_val_t) should not trigger a warning or crash clang-tidy.
6+
// A new expression forces Clang to implicitly declare std::align_val_t
7+
// inside namespace std when alignment is enabled. This test verifies
8+
// the checker handles this situation correctly in case of -system-headers
9+
// switch present. Situation without -system-headers is handled in
10+
// std-namespace-modification.cpp test file.
11+
12+
namespace std {}
13+
14+
// Trigger implicit std::align_val_t declaration.
15+
void *implicit_decl_test = new int;
16+
17+
namespace std {
18+
// CHECK-MESSAGES: :[[@LINE+2]]:5: warning: modification of 'std' namespace
19+
// CHECK-MESSAGES: :[[@LINE-2]]:11: note: 'std' namespace opened here
20+
int x;
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This test checks that the implicit std::align_val_t is created with no source
2+
// location and marked implicit when a new-expression triggers its synthesis.
3+
4+
// align_val_t is implicitly created in C++17+ (aligned allocation on by default).
5+
// RUN: %clang_cc1 -std=c++17 -ast-dump %s | FileCheck %s
6+
// RUN: %clang_cc1 -std=c++20 -ast-dump %s | FileCheck %s
7+
// RUN: %clang_cc1 -std=c++23 -ast-dump %s | FileCheck %s
8+
9+
// In older standards, -faligned-allocation must be explicit.
10+
// RUN: %clang_cc1 -std=c++03 -faligned-allocation -ast-dump %s | FileCheck %s
11+
// RUN: %clang_cc1 -std=c++11 -faligned-allocation -ast-dump %s | FileCheck %s
12+
// RUN: %clang_cc1 -std=c++14 -faligned-allocation -ast-dump %s | FileCheck %s
13+
14+
namespace std {}
15+
void *p = new int;
16+
17+
// CHECK: NamespaceDecl {{.*}} std
18+
// CHECK: EnumDecl {{.*}} <<invalid sloc>> <invalid sloc> implicit class align_val_t

0 commit comments

Comments
 (0)