From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- compilerplugins/clang/readability-redundant-pp.cxx | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 compilerplugins/clang/readability-redundant-pp.cxx (limited to 'compilerplugins/clang/readability-redundant-pp.cxx') diff --git a/compilerplugins/clang/readability-redundant-pp.cxx b/compilerplugins/clang/readability-redundant-pp.cxx new file mode 100644 index 000000000..dcfd70247 --- /dev/null +++ b/compilerplugins/clang/readability-redundant-pp.cxx @@ -0,0 +1,133 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +#include +#include + +#include "plugin.hxx" + +#include +#include +#include +#include +#include + +/// Finds preprocessor usage which is redundant (only #ifndef for now). + +namespace +{ +struct Entry +{ + clang::SourceLocation m_aLoc; + std::string m_aMacroName; +}; + +class RedundantPreprocessor : public clang::PPCallbacks, public loplugin::Plugin +{ +public: + explicit RedundantPreprocessor(const loplugin::InstantiationData& data); + virtual void run() override; + void Ifndef(clang::SourceLocation aLoc, const clang::Token& rMacroNameTok, + const clang::MacroDefinition& rMacroDefinition) override; + void Ifdef(clang::SourceLocation aLoc, const clang::Token& rMacroNameTok, + const clang::MacroDefinition& rMacroDefinition) override; + void Endif(clang::SourceLocation aLoc, clang::SourceLocation aIfLoc) override; + enum + { + isPPCallback = true + }; + +private: + clang::Preprocessor& m_rPP; + std::vector m_aDefStack; + std::vector m_aNotDefStack; +}; + +RedundantPreprocessor::RedundantPreprocessor(const loplugin::InstantiationData& data) + : Plugin(data) + , m_rPP(compiler.getPreprocessor()) +{ + compiler.getPreprocessor().addPPCallbacks(std::unique_ptr(this)); +} + +void RedundantPreprocessor::run() +{ + // nothing, only check preprocessor usage +} + +void RedundantPreprocessor::Ifdef(clang::SourceLocation aLoc, const clang::Token& rMacroNameTok, + const clang::MacroDefinition& /*rMacroDefinition*/) +{ + if (ignoreLocation(aLoc)) + return; + + std::string aMacroName = m_rPP.getSpelling(rMacroNameTok); + if (m_rPP.getSourceManager().isInMainFile(aLoc)) + { + for (const auto& rEntry : m_aDefStack) + { + if (rEntry.m_aMacroName == aMacroName) + { + report(DiagnosticsEngine::Warning, "nested ifdef", aLoc); + report(DiagnosticsEngine::Note, "previous ifdef", rEntry.m_aLoc); + } + } + } + + Entry aEntry; + aEntry.m_aLoc = aLoc; + aEntry.m_aMacroName = aMacroName; + m_aDefStack.push_back(aEntry); +} + +void RedundantPreprocessor::Ifndef(clang::SourceLocation aLoc, const clang::Token& rMacroNameTok, + const clang::MacroDefinition& /*rMacroDefinition*/) +{ + if (ignoreLocation(aLoc)) + return; + + std::string aMacroName = m_rPP.getSpelling(rMacroNameTok); + if (m_rPP.getSourceManager().isInMainFile(aLoc)) + { + for (const auto& rEntry : m_aNotDefStack) + { + if (rEntry.m_aMacroName == aMacroName) + { + report(DiagnosticsEngine::Warning, "nested ifndef", aLoc); + report(DiagnosticsEngine::Note, "previous ifndef", rEntry.m_aLoc); + } + } + } + + Entry aEntry; + aEntry.m_aLoc = aLoc; + aEntry.m_aMacroName = aMacroName; + m_aNotDefStack.push_back(aEntry); +} + +void RedundantPreprocessor::Endif(clang::SourceLocation /*aLoc*/, clang::SourceLocation aIfLoc) +{ + if (!m_aDefStack.empty()) + { + if (aIfLoc == m_aDefStack.back().m_aLoc) + m_aDefStack.pop_back(); + } + if (!m_aNotDefStack.empty()) + { + if (aIfLoc == m_aNotDefStack.back().m_aLoc) + m_aNotDefStack.pop_back(); + } +} + +loplugin::Plugin::Registration X("redundantpreprocessor"); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3