diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-11-07 08:58:45 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-11-07 10:49:22 +0100 |
commit | 13c247b7fee91b0ffb256e85bcd0cf416b3cd83e (patch) | |
tree | 7a1fe9cffafcab77b6fd1ca7e789a037c5201355 | |
parent | 511061b0d392f238a18a27477148731e3437b5ae (diff) |
new loplugin readability-redundant-pp
code originally from vmiklos, copied from dev-tools repo
Change-Id: Iadc2388fec888dde38cb4d59c78691dc1ea94c09
Reviewed-on: https://gerrit.libreoffice.org/62983
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | compilerplugins/clang/readability-redundant-pp.cxx | 129 | ||||
-rw-r--r-- | sw/source/core/doc/dbgoutsw.cxx | 2 |
2 files changed, 129 insertions, 2 deletions
diff --git a/compilerplugins/clang/readability-redundant-pp.cxx b/compilerplugins/clang/readability-redundant-pp.cxx new file mode 100644 index 000000000000..ed7fed1082c9 --- /dev/null +++ b/compilerplugins/clang/readability-redundant-pp.cxx @@ -0,0 +1,129 @@ +/* -*- 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 <iostream> +#include <stack> + +#include "plugin.hxx" + +#include <clang/Frontend/CompilerInstance.h> +#include <clang/Frontend/FrontendActions.h> +#include <clang/Tooling/CommonOptionsParser.h> +#include <clang/Tooling/Refactoring.h> +#include <llvm/Support/Signals.h> + +/// Finds preprocessor usage which is redundant (only #ifndef for now). + +namespace loplugin +{ +struct Entry +{ + clang::SourceLocation m_aLoc; + std::string m_aMacroName; +}; + +class RedundantPreprocessor : public clang::PPCallbacks, public Plugin +{ +public: + explicit RedundantPreprocessor(const 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; + +private: + clang::Preprocessor& m_rPP; + std::vector<Entry> m_aDefStack; + std::vector<Entry> m_aNotDefStack; +}; + +RedundantPreprocessor::RedundantPreprocessor(const InstantiationData& data) + : Plugin(data) + , m_rPP(compiler.getPreprocessor()) +{ + compiler.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(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; + + if (m_rPP.getSourceManager().isInMainFile(aLoc)) + { + std::string aMacroName = m_rPP.getSpelling(rMacroNameTok); + 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 = m_rPP.getSpelling(rMacroNameTok); + m_aDefStack.push_back(aEntry); +} + +void RedundantPreprocessor::Ifndef(clang::SourceLocation aLoc, const clang::Token& rMacroNameTok, + const clang::MacroDefinition& /*rMacroDefinition*/) +{ + if (ignoreLocation(aLoc)) + return; + + if (m_rPP.getSourceManager().isInMainFile(aLoc)) + { + std::string aMacroName = m_rPP.getSpelling(rMacroNameTok); + 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 = m_rPP.getSpelling(rMacroNameTok); + 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(); + } +} + +static Plugin::Registration<RedundantPreprocessor> X("redundantpreprocessor"); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/core/doc/dbgoutsw.cxx b/sw/source/core/doc/dbgoutsw.cxx index 9ffbc68866a0..9bd9ab22ef08 100644 --- a/sw/source/core/doc/dbgoutsw.cxx +++ b/sw/source/core/doc/dbgoutsw.cxx @@ -513,11 +513,9 @@ static OUString lcl_dbg_out(const SwNode & rNode) aTmpStr += OUString::number(rNode.GetIndex()); aTmpStr += "\""; -#ifdef DBG_UTIL aTmpStr += " serial=\""; aTmpStr += OUString::number(rNode.GetSerial()); aTmpStr += "\""; -#endif aTmpStr += " type=\""; aTmpStr += OUString::number(sal_Int32( rNode.GetNodeType() ) ); |