Skip to content

Commit 7f29f14

Browse files
committed
[clang-tidy] Ignore unevaluated context in cppcoreguidelines-pro-type-vararg
Ignore decltype, sizeof, alignof in this check. Fixes: llvm#30542 Reviewed By: ccotter Differential Revision: https://reviews.llvm.org/D157376
1 parent 8a15bdb commit 7f29f14

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeVarargCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ProTypeVarargCheck.h"
10+
#include "../utils/Matchers.h"
1011
#include "clang/AST/ASTContext.h"
1112
#include "clang/ASTMatchers/ASTMatchFinder.h"
1213
#include "clang/ASTMatchers/ASTMatchers.h"
@@ -133,7 +134,9 @@ void ProTypeVarargCheck::registerMatchers(MatchFinder *Finder) {
133134

134135
Finder->addMatcher(
135136
callExpr(callee(functionDecl(isVariadic(),
136-
unless(hasAnyName(AllowedVariadics)))))
137+
unless(hasAnyName(AllowedVariadics)))),
138+
unless(hasAncestor(expr(matchers::hasUnevaluatedContext()))),
139+
unless(hasAncestor(typeLoc())))
137140
.bind("callvararg"),
138141
this);
139142

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ Changes in existing checks
176176
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
177177
ignore delegate constructors.
178178

179+
- Improved :doc:`cppcoreguidelines-pro-type-vararg
180+
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check to ignore
181+
false-positives in unevaluated context (e.g., ``decltype``, ``sizeof``, ...).
182+
179183
- Improved :doc:`llvm-namespace-comment
180184
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
181185
``inline`` namespaces in the same format as :program:`clang-format`.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/pro-type-vararg.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ This check flags all calls to c-style vararg functions and all use of
77
``va_arg``.
88

99
To allow for SFINAE use of vararg functions, a call is not flagged if a literal
10-
0 is passed as the only vararg argument.
10+
0 is passed as the only vararg argument or function is used in unevaluated
11+
context.
1112

1213
Passing to varargs assumes the correct type will be read. This is fragile
1314
because it cannot generally be enforced to be safe in the language and so relies

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-type-vararg.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,16 @@ void no_false_positive_desugar_va_list(char *in) {
6666
char *tmp1 = in;
6767
void *tmp2 = in;
6868
}
69+
70+
namespace PR30542 {
71+
struct X;
72+
template <typename T>
73+
char IsNullConstant(X*);
74+
template <typename T>
75+
char (&IsNullConstant(...))[2];
76+
77+
static_assert(sizeof(IsNullConstant<int>(0)) == 1, "");
78+
static_assert(sizeof(IsNullConstant<int>(17)) == 2, "");
79+
80+
using Type = decltype(IsNullConstant<int>(17));
81+
}

0 commit comments

Comments
 (0)