Skip to content

Commit

Permalink
[clang-tidy] Ignore unevaluated context in cppcoreguidelines-pro-type…
Browse files Browse the repository at this point in the history
…-vararg

Ignore decltype, sizeof, alignof in this check.

Fixes: #30542

Reviewed By: ccotter

Differential Revision: https://reviews.llvm.org/D157376
  • Loading branch information
PiotrZSL committed Aug 10, 2023
1 parent 8a15bdb commit 7f29f14
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "ProTypeVarargCheck.h"
#include "../utils/Matchers.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
Expand Down Expand Up @@ -133,7 +134,9 @@ void ProTypeVarargCheck::registerMatchers(MatchFinder *Finder) {

Finder->addMatcher(
callExpr(callee(functionDecl(isVariadic(),
unless(hasAnyName(AllowedVariadics)))))
unless(hasAnyName(AllowedVariadics)))),
unless(hasAncestor(expr(matchers::hasUnevaluatedContext()))),
unless(hasAncestor(typeLoc())))
.bind("callvararg"),
this);

Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ Changes in existing checks
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
ignore delegate constructors.

- Improved :doc:`cppcoreguidelines-pro-type-vararg
<clang-tidy/checks/cppcoreguidelines/pro-type-vararg>` check to ignore
false-positives in unevaluated context (e.g., ``decltype``, ``sizeof``, ...).

- Improved :doc:`llvm-namespace-comment
<clang-tidy/checks/llvm/namespace-comment>` check to provide fixes for
``inline`` namespaces in the same format as :program:`clang-format`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ This check flags all calls to c-style vararg functions and all use of
``va_arg``.

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

Passing to varargs assumes the correct type will be read. This is fragile
because it cannot generally be enforced to be safe in the language and so relies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ void no_false_positive_desugar_va_list(char *in) {
char *tmp1 = in;
void *tmp2 = in;
}

namespace PR30542 {
struct X;
template <typename T>
char IsNullConstant(X*);
template <typename T>
char (&IsNullConstant(...))[2];

static_assert(sizeof(IsNullConstant<int>(0)) == 1, "");
static_assert(sizeof(IsNullConstant<int>(17)) == 2, "");

using Type = decltype(IsNullConstant<int>(17));
}

0 comments on commit 7f29f14

Please sign in to comment.