|
| 1 | +//===--- TrueMacroCheck.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 "TrueMacroCheck.h" |
| 10 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 11 | +#include "clang/Lex/MacroInfo.h" |
| 12 | +#include "clang/Lex/PPCallbacks.h" |
| 13 | +#include "clang/Lex/Preprocessor.h" |
| 14 | +#include <iostream> |
| 15 | + |
| 16 | +using namespace clang::ast_matchers; |
| 17 | + |
| 18 | +namespace clang::tidy::bugprone { |
| 19 | +namespace { |
| 20 | + |
| 21 | +class MacroCallback : public PPCallbacks { |
| 22 | + static constexpr const char *TrueMacroSpelling = "true"; |
| 23 | + |
| 24 | +public: |
| 25 | + MacroCallback(TrueMacroCheck *Check, const SourceManager &SM, |
| 26 | + Preprocessor *PP) |
| 27 | + : Check(Check), SM(&SM), PP(PP) {} |
| 28 | + void MacroDefined(const Token &MacroNameTok, |
| 29 | + const MacroDirective *MD) override { |
| 30 | + if (PP->getSpelling(MacroNameTok) == TrueMacroSpelling) |
| 31 | + TrueDefined = true; |
| 32 | + } |
| 33 | + |
| 34 | + virtual void MacroUndefined(const Token &MacroNameTok, |
| 35 | + const MacroDefinition &MD, |
| 36 | + const MacroDirective *Undef) override { |
| 37 | + if (PP->getSpelling(MacroNameTok) == TrueMacroSpelling) |
| 38 | + TrueDefined = false; |
| 39 | + } |
| 40 | + |
| 41 | + virtual void If(SourceLocation Loc, SourceRange ConditionRange, |
| 42 | + ConditionValueKind ConditionValue) override { |
| 43 | + StringRef Condition = |
| 44 | + Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange), |
| 45 | + PP->getSourceManager(), PP->getLangOpts()); |
| 46 | + |
| 47 | + for (auto &&Identifier : identifiersInCondition(Condition)) |
| 48 | + std::cout << Identifier.str() << ' ' << Identifier.size() << '\n'; |
| 49 | + } |
| 50 | + |
| 51 | +private: |
| 52 | + void emitDiagnostic() {} |
| 53 | + |
| 54 | + std::vector<StringRef> identifiersInCondition(StringRef Condition) { |
| 55 | + const static auto Start = [](char C) { |
| 56 | + return C == '_' || ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z'); |
| 57 | + }; |
| 58 | + |
| 59 | + const static auto Continue = [](char C) { |
| 60 | + return ('0' <= C && C <= '9') || Start(C); |
| 61 | + }; |
| 62 | + |
| 63 | + std::vector<StringRef> Identifiers; |
| 64 | + const char *Str = nullptr; |
| 65 | + |
| 66 | + for (size_t I = 0; I < Condition.size(); ++I) { |
| 67 | + char C = Condition[I]; |
| 68 | + |
| 69 | + if (!Str && Start(C)) { |
| 70 | + Str = Condition.begin() + I; |
| 71 | + } else if (Str && !Continue(C)) { |
| 72 | + Identifiers.emplace_back(Str, Condition.begin() + I - Str); |
| 73 | + Str = nullptr; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (Str) |
| 78 | + Identifiers.emplace_back(Str, Condition.end() - Str); |
| 79 | + |
| 80 | + return Identifiers; |
| 81 | + } |
| 82 | + |
| 83 | + bool TrueDefined = false; |
| 84 | + |
| 85 | + TrueMacroCheck *Check; |
| 86 | + const SourceManager *SM; |
| 87 | + Preprocessor *PP; |
| 88 | +}; |
| 89 | +} // namespace |
| 90 | + |
| 91 | +void TrueMacroCheck::registerPPCallbacks(const SourceManager &SM, |
| 92 | + Preprocessor *PP, |
| 93 | + Preprocessor *ModuleExpanderPP) { |
| 94 | + PP->addPPCallbacks(std::make_unique<MacroCallback>(this, SM, PP)); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace clang::tidy::bugprone |
0 commit comments