diff options
| author | Florin Iucha <florin@signbit.net> | 2019-12-07 12:33:10 -0500 |
|---|---|---|
| committer | Aaron Ballman <aaron@aaronballman.com> | 2019-12-07 12:33:10 -0500 |
| commit | 6dcb1003f2022cba36e9f5a6d39648c3a3a213a0 (patch) | |
| tree | e7087d113c9aaf2e89e85854c92842e37e068809 /clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp | |
| parent | c25de56905b104fb2b90559ce5863f4fec93a974 (diff) | |
| download | bcm5719-llvm-6dcb1003f2022cba36e9f5a6d39648c3a3a213a0.tar.gz bcm5719-llvm-6dcb1003f2022cba36e9f5a6d39648c3a3a213a0.zip | |
Optionally exclude bitfield definitions from magic numbers check
Adds the IgnoreBitFieldsWidths option to readability-magic-numbers.
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp index 39aaf89901f..6f6366cab6f 100644 --- a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp @@ -21,12 +21,12 @@ using namespace clang::ast_matchers; using namespace clang::ast_type_traits; -namespace { +namespace clang { -bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, - const DynTypedNode &Node) { +static bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, + const DynTypedNode &Node) { - const auto *AsDecl = Node.get<clang::DeclaratorDecl>(); + const auto *AsDecl = Node.get<DeclaratorDecl>(); if (AsDecl) { if (AsDecl->getType().isConstQualified()) return true; @@ -34,7 +34,7 @@ bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, return AsDecl->isImplicit(); } - if (Node.get<clang::EnumConstantDecl>() != nullptr) + if (Node.get<EnumConstantDecl>() != nullptr) return true; return llvm::any_of(Result.Context->getParents(Node), @@ -43,9 +43,18 @@ bool isUsedToInitializeAConstant(const MatchFinder::MatchResult &Result, }); } -} // namespace +static bool isUsedToDefineABitField(const MatchFinder::MatchResult &Result, + const DynTypedNode &Node) { + const auto *AsFieldDecl = Node.get<FieldDecl>(); + if (AsFieldDecl && AsFieldDecl->isBitField()) + return true; + + return llvm::any_of(Result.Context->getParents(Node), + [&Result](const DynTypedNode &Parent) { + return isUsedToDefineABitField(Result, Parent); + }); +} -namespace clang { namespace tidy { namespace readability { @@ -56,6 +65,7 @@ MagicNumbersCheck::MagicNumbersCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), IgnoreAllFloatingPointValues( Options.get("IgnoreAllFloatingPointValues", false)), + IgnoreBitFieldsWidths(Options.get("IgnoreBitFieldsWidths", true)), IgnorePowersOf2IntegerValues( Options.get("IgnorePowersOf2IntegerValues", false)) { // Process the set of ignored integer values. @@ -165,6 +175,16 @@ bool MagicNumbersCheck::isSyntheticValue(const SourceManager *SourceManager, return BufferIdentifier.empty(); } +bool MagicNumbersCheck::isBitFieldWidth( + const clang::ast_matchers::MatchFinder::MatchResult &Result, + const IntegerLiteral &Literal) const { + return IgnoreBitFieldsWidths && + llvm::any_of(Result.Context->getParents(Literal), + [&Result](const DynTypedNode &Parent) { + return isUsedToDefineABitField(Result, Parent); + }); +} + } // namespace readability } // namespace tidy } // namespace clang |

