diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -38,6 +38,7 @@ #include "NotNullTerminatedResultCheck.h" #include "ParentVirtualCallCheck.h" #include "PosixReturnCheck.h" +#include "ProblematicStaticsCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" #include "SignedCharMisuseCheck.h" @@ -131,6 +132,8 @@ "bugprone-parent-virtual-call"); CheckFactories.registerCheck( "bugprone-posix-return"); + CheckFactories.registerCheck( + "bugprone-problematic-statics"); CheckFactories.registerCheck( "bugprone-reserved-identifier"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -33,6 +33,7 @@ NotNullTerminatedResultCheck.cpp ParentVirtualCallCheck.cpp PosixReturnCheck.cpp + ProblematicStaticsCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp SignedCharMisuseCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ProblematicStaticsCheck.h b/clang-tools-extra/clang-tidy/bugprone/ProblematicStaticsCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ProblematicStaticsCheck.h @@ -0,0 +1,32 @@ +//===--- ProblematicStaticsCheck.h - clang-tidy ----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_PROBLEMATIC_STATICS_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_PROBLEMATIC_STATICS_CHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// Looks for static variables in headers that may pose problems for users in +/// other shared libraries. +class ProblematicStaticsCheck : public ClangTidyCheck { +public: + using ClangTidyCheck::ClangTidyCheck; + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_PROBLEMATIC_STATICS_CHECK_H diff --git a/clang-tools-extra/clang-tidy/bugprone/ProblematicStaticsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ProblematicStaticsCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ProblematicStaticsCheck.cpp @@ -0,0 +1,40 @@ +//===--- ProblematicStaticsCheck.cpp - clang-tidy -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ProblematicStaticsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace bugprone { + +void ProblematicStaticsCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + returnStmt(hasReturnValue(ignoringImplicit(unaryOperator( + hasOperatorName("&"), + hasUnaryOperand(declRefExpr( + to(varDecl(isStaticLocal()).bind("var"))))))), + unless(isExpansionInMainFile())) + .bind("return"), + this); +} + +void ProblematicStaticsCheck::check(const MatchFinder::MatchResult &Result) { + const auto *VD = Result.Nodes.getNodeAs("var"); + const auto *Return = Result.Nodes.getNodeAs("return"); + diag(Return->getBeginLoc(), "address of static local variable %0 may not " + "be identical across library boundaries") + << VD; +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone-problematic-statics.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone-problematic-statics.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone-problematic-statics.rst @@ -0,0 +1,17 @@ +.. title:: clang-tidy - bugprone-problematic-statics + +bugprone-problematic-statics +============================ + +Detects functions defined in headers that return the address of a static +local variable. These are not guaranteed to have the same addresss across +shared libraries and could be problematic for plugins. + +.. code-block:: c++ + + struct ProgramStateTrait { + static void *GDMIndex() { + static int index = 0; + return &index; + } + }; diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -75,6 +75,7 @@ `bugprone-not-null-terminated-result `_, "Yes" `bugprone-parent-virtual-call `_, "Yes" `bugprone-posix-return `_, "Yes" + `bugprone-problematic-statics `_, `bugprone-redundant-branch-condition `_, "Yes" `bugprone-reserved-identifier `_, "Yes" `bugprone-signed-char-misuse `_, diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/bugprone-problematic-statics/bugprone-problematic-statics.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/bugprone-problematic-statics/bugprone-problematic-statics.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/bugprone-problematic-statics/bugprone-problematic-statics.h @@ -0,0 +1,11 @@ +#ifndef PROBLEMATIC_STATICS_H +#define PROBLEMATIC_STATICS_H + +struct ProgramStateTrait { + static void *GDMIndex() { + static int index = 0; + return &index; + } +}; + +#endif diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-problematic-statics.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-problematic-statics.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-problematic-statics.cpp @@ -0,0 +1,11 @@ +// RUN: %check_clang_tidy %s bugprone-problematic-statics %t -- -header-filter='.*' -- -I %S/Inputs/bugprone-problematic-statics + +#include "bugprone-problematic-statics.h" +// CHECK-MESSAGES: bugprone-problematic-statics.h:7:5: warning: address of static local variable 'index' may not be identical across library boundaries [bugprone-problematic-statics] + +struct ProgramStateTraitInMain { + static void *GDMIndex() { + static int index = 0; + return &index; + } +};