Skip to content

Commit 08dd4e6

Browse files
SwSw
Sw
authored and
Sw
committed
Merged master:7b74b0d4e54 into amd-gfx:46601da50bc
Local branch amd-gfx 46601da Reinstate getSplat(unsigned NumElts, Constant *V) Remote branch master 7b74b0d [llvm-objdump] --syms: print 'u' for STB_GNU_UNIQUE
2 parents 46601da + 7b74b0d commit 08dd4e6

File tree

68 files changed

+1616
-490
lines changed

Some content is hidden

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

68 files changed

+1616
-490
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===--- AvoidNonConstGlobalVariablesCheck.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 "AvoidNonConstGlobalVariablesCheck.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
13+
14+
using namespace clang::ast_matchers;
15+
16+
namespace clang {
17+
namespace tidy {
18+
namespace cppcoreguidelines {
19+
20+
void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) {
21+
auto GlobalVariable = varDecl(
22+
hasGlobalStorage(),
23+
unless(anyOf(
24+
isConstexpr(), hasType(isConstQualified()),
25+
hasType(referenceType())))); // References can't be changed, only the
26+
// data they reference can be changed.
27+
28+
auto GlobalReferenceToNonConst =
29+
varDecl(hasGlobalStorage(), hasType(referenceType()),
30+
unless(hasType(references(qualType(isConstQualified())))));
31+
32+
auto GlobalPointerToNonConst =
33+
varDecl(hasGlobalStorage(),
34+
hasType(pointerType(pointee(unless(isConstQualified())))));
35+
36+
Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this);
37+
Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"),
38+
this);
39+
Finder->addMatcher(GlobalPointerToNonConst.bind("indirection_to_non-const"),
40+
this);
41+
}
42+
43+
void AvoidNonConstGlobalVariablesCheck::check(
44+
const MatchFinder::MatchResult &Result) {
45+
46+
if (const auto *Variable =
47+
Result.Nodes.getNodeAs<VarDecl>("non-const_variable")) {
48+
diag(Variable->getLocation(), "variable %0 is non-const and globally "
49+
"accessible, consider making it const")
50+
<< Variable; // FIXME: Add fix-it hint to Variable
51+
// Don't return early, a non-const variable may also be a pointer or
52+
// reference to non-const data.
53+
}
54+
55+
if (const auto *VD =
56+
Result.Nodes.getNodeAs<VarDecl>("indirection_to_non-const")) {
57+
diag(VD->getLocation(),
58+
"variable %0 provides global access to a non-const object; consider "
59+
"making the %select{referenced|pointed-to}1 data 'const'")
60+
<< VD
61+
<< VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable
62+
}
63+
}
64+
65+
} // namespace cppcoreguidelines
66+
} // namespace tidy
67+
} // namespace clang
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- AvoidNonConstGlobalVariablesCheck.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_CPPCOREGUIDELINES_AVOIDNONCONSTGLOBALVARIABLESCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDNONCONSTGLOBALVARIABLESCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace cppcoreguidelines {
17+
18+
/// Non-const global variables hide dependencies and make the dependencies
19+
/// subject to unpredictable changes.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-avoid-non-const-global-variables.html
23+
class AvoidNonConstGlobalVariablesCheck : public ClangTidyCheck {
24+
public:
25+
AvoidNonConstGlobalVariablesCheck(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 cppcoreguidelines
32+
} // namespace tidy
33+
} // namespace clang
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDNONCONSTGLOBALVARIABLESCHECK_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
22

33
add_clang_library(clangTidyCppCoreGuidelinesModule
44
AvoidGotoCheck.cpp
5+
AvoidNonConstGlobalVariablesCheck.cpp
56
CppCoreGuidelinesTidyModule.cpp
67
InitVariablesCheck.cpp
78
InterfacesGlobalInitCheck.cpp

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "../modernize/UseOverrideCheck.h"
1616
#include "../readability/MagicNumbersCheck.h"
1717
#include "AvoidGotoCheck.h"
18+
#include "AvoidNonConstGlobalVariablesCheck.h"
1819
#include "InitVariablesCheck.h"
1920
#include "InterfacesGlobalInitCheck.h"
2021
#include "MacroUsageCheck.h"
@@ -48,6 +49,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
4849
"cppcoreguidelines-avoid-goto");
4950
CheckFactories.registerCheck<readability::MagicNumbersCheck>(
5051
"cppcoreguidelines-avoid-magic-numbers");
52+
CheckFactories.registerCheck<AvoidNonConstGlobalVariablesCheck>(
53+
"cppcoreguidelines-avoid-non-const-global-variables");
5154
CheckFactories.registerCheck<modernize::UseOverrideCheck>(
5255
"cppcoreguidelines-explicit-virtual-functions");
5356
CheckFactories.registerCheck<InitVariablesCheck>(

clang-tools-extra/clangd/Preamble.cpp

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

99
#include "Preamble.h"
10+
#include "Compiler.h"
1011
#include "Logger.h"
1112
#include "Trace.h"
1213
#include "clang/Basic/SourceLocation.h"
@@ -75,21 +76,21 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
7576

7677
} // namespace
7778

78-
PreambleData::PreambleData(llvm::StringRef Version,
79+
PreambleData::PreambleData(const ParseInputs &Inputs,
7980
PrecompiledPreamble Preamble,
8081
std::vector<Diag> Diags, IncludeStructure Includes,
8182
MainFileMacros Macros,
8283
std::unique_ptr<PreambleFileStatusCache> StatCache,
8384
CanonicalIncludes CanonIncludes)
84-
: Version(Version), Preamble(std::move(Preamble)), Diags(std::move(Diags)),
85+
: Version(Inputs.Version), CompileCommand(Inputs.CompileCommand),
86+
Preamble(std::move(Preamble)), Diags(std::move(Diags)),
8587
Includes(std::move(Includes)), Macros(std::move(Macros)),
8688
StatCache(std::move(StatCache)), CanonIncludes(std::move(CanonIncludes)) {
8789
}
8890

8991
std::shared_ptr<const PreambleData>
9092
buildPreamble(PathRef FileName, CompilerInvocation &CI,
9193
std::shared_ptr<const PreambleData> OldPreamble,
92-
const tooling::CompileCommand &OldCompileCommand,
9394
const ParseInputs &Inputs, bool StoreInMemory,
9495
PreambleParsedCallback PreambleCallback) {
9596
// Note that we don't need to copy the input contents, preamble can live
@@ -100,7 +101,8 @@ buildPreamble(PathRef FileName, CompilerInvocation &CI,
100101
ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0);
101102

102103
if (OldPreamble &&
103-
compileCommandsAreEqual(Inputs.CompileCommand, OldCompileCommand) &&
104+
compileCommandsAreEqual(Inputs.CompileCommand,
105+
OldPreamble->CompileCommand) &&
104106
OldPreamble->Preamble.CanReuse(CI, ContentsBuffer.get(), Bounds,
105107
Inputs.FS.get())) {
106108
vlog("Reusing preamble version {0} for version {1} of {2}",
@@ -155,7 +157,7 @@ buildPreamble(PathRef FileName, CompilerInvocation &CI,
155157
BuiltPreamble->getSize(), FileName, Inputs.Version);
156158
std::vector<Diag> Diags = PreambleDiagnostics.take();
157159
return std::make_shared<PreambleData>(
158-
Inputs.Version, std::move(*BuiltPreamble), std::move(Diags),
160+
Inputs, std::move(*BuiltPreamble), std::move(Diags),
159161
SerializedDeclsCollector.takeIncludes(),
160162
SerializedDeclsCollector.takeMacros(), std::move(StatCache),
161163
SerializedDeclsCollector.takeCanonicalIncludes());

clang-tools-extra/clangd/Preamble.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace clangd {
4343
/// As we must avoid re-parsing the preamble, any information that can only
4444
/// be obtained during parsing must be eagerly captured and stored here.
4545
struct PreambleData {
46-
PreambleData(llvm::StringRef Version, PrecompiledPreamble Preamble,
46+
PreambleData(const ParseInputs &Inputs, PrecompiledPreamble Preamble,
4747
std::vector<Diag> Diags, IncludeStructure Includes,
4848
MainFileMacros Macros,
4949
std::unique_ptr<PreambleFileStatusCache> StatCache,
@@ -80,7 +80,6 @@ using PreambleParsedCallback =
8080
std::shared_ptr<const PreambleData>
8181
buildPreamble(PathRef FileName, CompilerInvocation &CI,
8282
std::shared_ptr<const PreambleData> OldPreamble,
83-
const tooling::CompileCommand &OldCompileCommand,
8483
const ParseInputs &Inputs, bool StoreInMemory,
8584
PreambleParsedCallback PreambleCallback);
8685

clang-tools-extra/clangd/TUScheduler.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) {
403403
std::tie(PrevInputs->CompileCommand, PrevInputs->Contents) ==
404404
std::tie(Inputs.CompileCommand, Inputs.Contents);
405405

406-
tooling::CompileCommand OldCommand = PrevInputs->CompileCommand;
407406
bool RanCallbackForPrevInputs = RanASTCallback;
408407
{
409408
std::lock_guard<std::mutex> Lock(Mutex);
@@ -445,8 +444,7 @@ void ASTWorker::update(ParseInputs Inputs, WantDiagnostics WantDiags) {
445444
Inputs.ForceRebuild ? std::shared_ptr<const PreambleData>()
446445
: getPossiblyStalePreamble();
447446
std::shared_ptr<const PreambleData> NewPreamble = buildPreamble(
448-
FileName, *Invocation, OldPreamble, OldCommand, Inputs,
449-
StorePreambleInMemory,
447+
FileName, *Invocation, OldPreamble, Inputs, StorePreambleInMemory,
450448
[this, Version(Inputs.Version)](
451449
ASTContext &Ctx, std::shared_ptr<clang::Preprocessor> PP,
452450
const CanonicalIncludes &CanonIncludes) {

clang-tools-extra/clangd/unittests/FileIndexTests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,16 @@ TEST(FileIndexTest, RebuildWithPreamble) {
286286

287287
FileIndex Index;
288288
bool IndexUpdated = false;
289-
buildPreamble(
290-
FooCpp, *CI, /*OldPreamble=*/nullptr, tooling::CompileCommand(), PI,
291-
/*StoreInMemory=*/true,
292-
[&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP,
293-
const CanonicalIncludes &CanonIncludes) {
294-
EXPECT_FALSE(IndexUpdated) << "Expected only a single index update";
295-
IndexUpdated = true;
296-
Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx, std::move(PP),
297-
CanonIncludes);
298-
});
289+
buildPreamble(FooCpp, *CI, /*OldPreamble=*/nullptr, PI,
290+
/*StoreInMemory=*/true,
291+
[&](ASTContext &Ctx, std::shared_ptr<Preprocessor> PP,
292+
const CanonicalIncludes &CanonIncludes) {
293+
EXPECT_FALSE(IndexUpdated)
294+
<< "Expected only a single index update";
295+
IndexUpdated = true;
296+
Index.updatePreamble(FooCpp, /*Version=*/"null", Ctx,
297+
std::move(PP), CanonIncludes);
298+
});
299299
ASSERT_TRUE(IndexUpdated);
300300

301301
// Check the index contains symbols from the preamble, but not from the main

clang-tools-extra/clangd/unittests/TestTU.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ ParsedAST TestTU::build() const {
6767
assert(CI && "Failed to build compilation invocation.");
6868
auto Preamble =
6969
buildPreamble(FullFilename, *CI,
70-
/*OldPreamble=*/nullptr,
71-
/*OldCompileCommand=*/Inputs.CompileCommand, Inputs,
70+
/*OldPreamble=*/nullptr, Inputs,
7271
/*StoreInMemory=*/true, /*PreambleCallback=*/nullptr);
7372
auto AST =
7473
buildAST(FullFilename, std::move(CI), Diags.take(), Inputs, Preamble);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ New module
7575

7676
New checks
7777
^^^^^^^^^^
78+
- New :doc:`cppcoreguidelines-avoid-non-const-global-variables
79+
<clang-tidy/checks/cppcoreguidelines-avoid-non-const-global-variables>` check.
80+
Finds non-const global variables as described in check I.2 of C++ Core
81+
Guidelines.
7882

7983
- New :doc:`bugprone-misplaced-pointer-arithmetic-in-alloc
8084
<clang-tidy/checks/bugprone-misplaced-pointer-arithmetic-in-alloc>` check.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. title:: clang-tidy - cppcoreguidelines-avoid-non-const-global-variables
2+
3+
cppcoreguidelines-avoid-non-const-global-variables
4+
==================================================
5+
6+
Finds non-const global variables as described in `I.2 of C++ Core Guidelines <https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ri-global>` .
7+
As `R.6 of C++ Core Guidelines <https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-global>` is a duplicate of rule I.2 it also covers that rule.
8+
9+
.. code-block:: c++
10+
11+
char a; // Warns!
12+
const char b = 0;
13+
14+
namespace some_namespace
15+
{
16+
char c; // Warns!
17+
const char d = 0;
18+
}
19+
20+
char * c_ptr1 = &some_namespace::c; // Warns!
21+
char *const c_const_ptr = &some_namespace::c; // Warns!
22+
char & c_reference = some_namespace::c; // Warns!
23+
24+
class Foo // No Warnings inside Foo, only namespace scope is covered
25+
{
26+
public:
27+
char e = 0;
28+
const char f = 0;
29+
protected:
30+
char g = 0;
31+
private:
32+
char h = 0;
33+
};
34+
35+
Variables: ``a``, ``c``, ``c_ptr1``, ``c_ptr2``, ``c_const_ptr`` and
36+
``c_reference``, will all generate warnings since they are either:
37+
a globally accessible variable and non-const, a pointer or reference providing
38+
global access to non-const data or both.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ Clang-Tidy Checks
380380
`clang-analyzer-unix.cstring.NullArg <clang-analyzer-unix.cstring.NullArg.html>`_, `Clang Static Analyzer <https://clang.llvm.org/docs/analyzer/checkers.html>`_,
381381
`cppcoreguidelines-avoid-c-arrays <cppcoreguidelines-avoid-c-arrays.html>`_, `modernize-avoid-c-arrays <modernize-avoid-c-arrays.html>`_,
382382
`cppcoreguidelines-avoid-magic-numbers <cppcoreguidelines-avoid-magic-numbers.html>`_, `readability-magic-numbers <readability-magic-numbers.html>`_,
383+
`cppcoreguidelines-avoid-non-const-global-variables <cppcoreguidelines-avoid-non-const-global-variables.html>`_, , , ""
383384
`cppcoreguidelines-c-copy-assignment-signature <cppcoreguidelines-c-copy-assignment-signature.html>`_, `misc-unconventional-assign-operator <misc-unconventional-assign-operator.html>`_,
384385
`cppcoreguidelines-explicit-virtual-functions <cppcoreguidelines-explicit-virtual-functions.html>`_, `modernize-use-override <modernize-use-override.html>`_, "Yes"
385386
`cppcoreguidelines-non-private-member-variables-in-classes <cppcoreguidelines-non-private-member-variables-in-classes.html>`_, `misc-non-private-member-variables-in-classes <misc-non-private-member-variables-in-classes.html>`_,
@@ -413,3 +414,4 @@ Clang-Tidy Checks
413414
`hicpp-use-override <hicpp-use-override.html>`_, `modernize-use-override <modernize-use-override.html>`_, "Yes"
414415
`hicpp-vararg <hicpp-vararg.html>`_, `cppcoreguidelines-pro-type-vararg <cppcoreguidelines-pro-type-vararg.html>`_,
415416
`llvm-qualified-auto <llvm-qualified-auto.html>`_, `readability-qualified-auto <readability-qualified-auto.html>`_, "Yes"
417+

0 commit comments

Comments
 (0)