Skip to content

Commit 9827dc4

Browse files
authored
LLVM and SPIRV-LLVM-Translator pulldown (WW36)
LLVM: llvm/llvm-project@83ddfa0 SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@894f95b
2 parents c23fe4b + ae96b52 commit 9827dc4

File tree

3,202 files changed

+159019
-73189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,202 files changed

+159019
-73189
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "StringLiteralWithEmbeddedNulCheck.h"
5353
#include "SuspiciousEnumUsageCheck.h"
5454
#include "SuspiciousIncludeCheck.h"
55+
#include "SuspiciousMemoryComparisonCheck.h"
5556
#include "SuspiciousMemsetUsageCheck.h"
5657
#include "SuspiciousMissingCommaCheck.h"
5758
#include "SuspiciousSemicolonCheck.h"
@@ -160,6 +161,8 @@ class BugproneModule : public ClangTidyModule {
160161
"bugprone-suspicious-enum-usage");
161162
CheckFactories.registerCheck<SuspiciousIncludeCheck>(
162163
"bugprone-suspicious-include");
164+
CheckFactories.registerCheck<SuspiciousMemoryComparisonCheck>(
165+
"bugprone-suspicious-memory-comparison");
163166
CheckFactories.registerCheck<SuspiciousMemsetUsageCheck>(
164167
"bugprone-suspicious-memset-usage");
165168
CheckFactories.registerCheck<SuspiciousMissingCommaCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ add_clang_library(clangTidyBugproneModule
4747
StringLiteralWithEmbeddedNulCheck.cpp
4848
SuspiciousEnumUsageCheck.cpp
4949
SuspiciousIncludeCheck.cpp
50+
SuspiciousMemoryComparisonCheck.cpp
5051
SuspiciousMemsetUsageCheck.cpp
5152
SuspiciousMissingCommaCheck.cpp
5253
SuspiciousSemicolonCheck.cpp

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static const std::string DefaultIgnoredParameterTypeSuffixes =
5656
"reverse_const_iterator",
5757
"ConstReverseIterator",
5858
"Const_Reverse_Iterator",
59-
"const_reverse_iterator"
59+
"const_reverse_iterator",
6060
"Constreverseiterator",
6161
"constreverseiterator"});
6262

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===--- SuspiciousMemoryComparisonCheck.cpp - clang-tidy -----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "SuspiciousMemoryComparisonCheck.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
13+
using namespace clang::ast_matchers;
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace bugprone {
18+
19+
static llvm::Optional<uint64_t> tryEvaluateSizeExpr(const Expr *SizeExpr,
20+
const ASTContext &Ctx) {
21+
Expr::EvalResult Result;
22+
if (SizeExpr->EvaluateAsRValue(Result, Ctx))
23+
return Ctx.toBits(
24+
CharUnits::fromQuantity(Result.Val.getInt().getExtValue()));
25+
return None;
26+
}
27+
28+
void SuspiciousMemoryComparisonCheck::registerMatchers(MatchFinder *Finder) {
29+
Finder->addMatcher(
30+
callExpr(allOf(callee(namedDecl(
31+
anyOf(hasName("::memcmp"), hasName("::std::memcmp")))),
32+
unless(isInstantiationDependent())))
33+
.bind("call"),
34+
this);
35+
}
36+
37+
void SuspiciousMemoryComparisonCheck::check(
38+
const MatchFinder::MatchResult &Result) {
39+
const ASTContext &Ctx = *Result.Context;
40+
const auto *CE = Result.Nodes.getNodeAs<CallExpr>("call");
41+
42+
const Expr *SizeExpr = CE->getArg(2);
43+
assert(SizeExpr != nullptr && "Third argument of memcmp is mandatory.");
44+
llvm::Optional<uint64_t> ComparedBits = tryEvaluateSizeExpr(SizeExpr, Ctx);
45+
46+
for (unsigned int ArgIndex = 0; ArgIndex < 2; ++ArgIndex) {
47+
const Expr *ArgExpr = CE->getArg(ArgIndex);
48+
QualType ArgType = ArgExpr->IgnoreImplicit()->getType();
49+
const Type *PointeeType = ArgType->getPointeeOrArrayElementType();
50+
assert(PointeeType != nullptr && "PointeeType should always be available.");
51+
QualType PointeeQualifiedType(PointeeType, 0);
52+
53+
if (PointeeType->isRecordType()) {
54+
if (const RecordDecl *RD =
55+
PointeeType->getAsRecordDecl()->getDefinition()) {
56+
if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) {
57+
if (!CXXDecl->isStandardLayout()) {
58+
diag(CE->getBeginLoc(),
59+
"comparing object representation of non-standard-layout type "
60+
"%0; consider using a comparison operator instead")
61+
<< PointeeQualifiedType;
62+
break;
63+
}
64+
}
65+
}
66+
}
67+
68+
if (!PointeeType->isIncompleteType()) {
69+
uint64_t PointeeSize = Ctx.getTypeSize(PointeeType);
70+
if (ComparedBits.hasValue() && *ComparedBits >= PointeeSize &&
71+
!Ctx.hasUniqueObjectRepresentations(PointeeQualifiedType)) {
72+
diag(CE->getBeginLoc(),
73+
"comparing object representation of type %0 which does not have a "
74+
"unique object representation; consider comparing %select{the "
75+
"values|the members of the object}1 manually")
76+
<< PointeeQualifiedType << (PointeeType->isRecordType() ? 1 : 0);
77+
break;
78+
}
79+
}
80+
}
81+
}
82+
83+
} // namespace bugprone
84+
} // namespace tidy
85+
} // namespace clang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- SuspiciousMemoryComparisonCheck.h - clang-tidy ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SUSPICIOUSMEMORYCOMPARISONCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SUSPICIOUSMEMORYCOMPARISONCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace bugprone {
17+
18+
/// Finds potentially incorrect calls to ``memcmp()`` based on properties of the
19+
/// arguments.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-suspicious-memory-comparison.html
23+
class SuspiciousMemoryComparisonCheck : public ClangTidyCheck {
24+
public:
25+
SuspiciousMemoryComparisonCheck(StringRef Name, ClangTidyContext *Context)
26+
: ClangTidyCheck(Name, Context) {}
27+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
};
30+
31+
} // namespace bugprone
32+
} // namespace tidy
33+
} // namespace clang
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SUSPICIOUSMEMORYCOMPARISONCHECK_H

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../bugprone/SignalHandlerCheck.h"
1515
#include "../bugprone/SignedCharMisuseCheck.h"
1616
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
17+
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
1718
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
1819
#include "../concurrency/ThreadCanceltypeAsynchronousCheck.h"
1920
#include "../google/UnnamedNamespaceInHeaderCheck.h"
@@ -98,8 +99,13 @@ class CERTModule : public ClangTidyModule {
9899
"cert-dcl37-c");
99100
// ENV
100101
CheckFactories.registerCheck<CommandProcessorCheck>("cert-env33-c");
102+
// EXP
103+
CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
104+
"cert-exp42-c");
101105
// FLP
102106
CheckFactories.registerCheck<FloatLoopCounter>("cert-flp30-c");
107+
CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
108+
"cert-flp37-c");
103109
// FIO
104110
CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
105111
// ERR

clang-tools-extra/docs/ReleaseNotes.rst

+15
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ The improvements are...
7272
New checks
7373
^^^^^^^^^^
7474

75+
- New :doc:`bugprone-suspicious-memory-comparison
76+
<clang-tidy/checks/bugprone-suspicious-memory-comparison>` check.
77+
78+
Finds potentially incorrect calls to ``memcmp()`` based on properties of the arguments.
79+
7580
- New :doc:`readability-identifier-length
7681
<clang-tidy/checks/readability-identifier-length>` check.
7782

@@ -80,6 +85,16 @@ New checks
8085
New check aliases
8186
^^^^^^^^^^^^^^^^^
8287

88+
- New alias :doc:`cert-exp42-c
89+
<clang-tidy/checks/cert-exp42-c>` to
90+
:doc:`bugprone-suspicious-memory-comparison
91+
<clang-tidy/checks/bugprone-suspicious-memory-comparison>` was added.
92+
93+
- New alias :doc:`cert-flp37-c
94+
<clang-tidy/checks/cert-flp37-c>` to
95+
:doc:`bugprone-suspicious-memory-comparison
96+
<clang-tidy/checks/bugprone-suspicious-memory-comparison>` was added.
97+
8398
Changes in existing checks
8499
^^^^^^^^^^^^^^^^^^^^^^^^^^
85100

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. title:: clang-tidy - bugprone-suspicious-memory-comparison
2+
3+
bugprone-suspicious-memory-comparison
4+
=====================================
5+
6+
Finds potentially incorrect calls to ``memcmp()`` based on properties of the
7+
arguments. The following cases are covered:
8+
9+
**Case 1: Non-standard-layout type**
10+
11+
Comparing the object representaions of non-standard-layout objects may not
12+
properly compare the value representations.
13+
14+
**Case 2: Types with no unique object representation**
15+
16+
Objects with the same value may not have the same object representaion.
17+
This may be caused by padding or floating-point types.
18+
19+
See also:
20+
`EXP42-C. Do not compare padding data
21+
<https://wiki.sei.cmu.edu/confluence/display/c/EXP42-C.+Do+not+compare+padding+data>`_
22+
and
23+
`FLP37-C. Do not use object representations to compare floating-point values
24+
<https://wiki.sei.cmu.edu/confluence/display/c/FLP37-C.+Do+not+use+object+representations+to+compare+floating-point+values>`_
25+
26+
This check is also related to and partially overlaps the CERT C++ Coding Standard rules
27+
`OOP57-CPP. Prefer special member functions and overloaded operators to C Standard Library functions
28+
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP57-CPP.+Prefer+special+member+functions+and+overloaded+operators+to+C+Standard+Library+functions>`_
29+
and
30+
`EXP62-CPP. Do not access the bits of an object representation that are not part of the object's value representation
31+
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/EXP62-CPP.+Do+not+access+the+bits+of+an+object+representation+that+are+not+part+of+the+object%27s+value+representation>`_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. meta::
2+
:http-equiv=refresh: 5;URL=bugprone-suspicious-memory-comparison.html
3+
4+
cert-exp42-c
5+
============
6+
7+
The cert-exp42-c check is an alias, please see
8+
`bugprone-suspicious-memory-comparison <bugprone-suspicious-memory-comparison.html>`_ for more information.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. meta::
2+
:http-equiv=refresh: 5;URL=bugprone-suspicious-memory-comparison.html
3+
4+
cert-flp37-c
5+
============
6+
7+
The cert-flp37-c check is an alias, please see
8+
`bugprone-suspicious-memory-comparison <bugprone-suspicious-memory-comparison.html>`_ for more information.

clang-tools-extra/docs/clang-tidy/checks/list.rst

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Clang-Tidy Checks
9393
`bugprone-string-literal-with-embedded-nul <bugprone-string-literal-with-embedded-nul.html>`_,
9494
`bugprone-suspicious-enum-usage <bugprone-suspicious-enum-usage.html>`_,
9595
`bugprone-suspicious-include <bugprone-suspicious-include.html>`_,
96+
`bugprone-suspicious-memory-comparison <bugprone-suspicious-memory-comparison.html>`_,
9697
`bugprone-suspicious-memset-usage <bugprone-suspicious-memset-usage.html>`_, "Yes"
9798
`bugprone-suspicious-missing-comma <bugprone-suspicious-missing-comma.html>`_,
9899
`bugprone-suspicious-semicolon <bugprone-suspicious-semicolon.html>`_, "Yes"
@@ -117,7 +118,9 @@ Clang-Tidy Checks
117118
`cert-err52-cpp <cert-err52-cpp.html>`_,
118119
`cert-err58-cpp <cert-err58-cpp.html>`_,
119120
`cert-err60-cpp <cert-err60-cpp.html>`_,
121+
`cert-exp42-c <cert-exp42-c.html>`_,
120122
`cert-flp30-c <cert-flp30-c.html>`_,
123+
`cert-flp37-c <cert-flp37-c.html>`_,
121124
`cert-mem57-cpp <cert-mem57-cpp.html>`_,
122125
`cert-msc50-cpp <cert-msc50-cpp.html>`_,
123126
`cert-msc51-cpp <cert-msc51-cpp.html>`_,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %check_clang_tidy %s bugprone-suspicious-memory-comparison %t \
2+
// RUN: -- -- -target i386-unknown-unknown
3+
4+
static_assert(sizeof(int *) == sizeof(int));
5+
6+
namespace std {
7+
typedef __SIZE_TYPE__ size_t;
8+
int memcmp(const void *lhs, const void *rhs, size_t count);
9+
} // namespace std
10+
11+
namespace no_padding_on_32bit {
12+
struct S {
13+
int x;
14+
int *y;
15+
};
16+
17+
void test() {
18+
S a, b;
19+
std::memcmp(&a, &b, sizeof(S));
20+
}
21+
} // namespace no_padding_on_32bit
22+
23+
namespace inner_padding {
24+
struct S {
25+
char x;
26+
int y;
27+
};
28+
void test() {
29+
S a, b;
30+
std::memcmp(&a, &b, sizeof(S));
31+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'inner_padding::S' which does not have a unique object representation; consider comparing the members of the object manually
32+
}
33+
} // namespace inner_padding

0 commit comments

Comments
 (0)