2019-09-30 02:16:36 -05:00
|
|
|
/* -*- 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-01-17 11:38:37 -06:00
|
|
|
#include <cassert>
|
2019-09-30 02:16:36 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#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 duplicated preprocessor defines, which generally indicate that some definition
|
|
|
|
/// needs to be centralised somewhere.
|
|
|
|
|
2019-11-16 08:39:12 -06:00
|
|
|
namespace
|
2019-09-30 02:16:36 -05:00
|
|
|
{
|
|
|
|
struct Entry
|
|
|
|
{
|
|
|
|
clang::SourceLocation m_aLoc;
|
|
|
|
};
|
|
|
|
|
2019-11-16 08:39:12 -06:00
|
|
|
class DuplicateDefines : public clang::PPCallbacks, public loplugin::Plugin
|
2019-09-30 02:16:36 -05:00
|
|
|
{
|
|
|
|
public:
|
2019-11-16 08:39:12 -06:00
|
|
|
explicit DuplicateDefines(const loplugin::InstantiationData& data);
|
2019-09-30 02:16:36 -05:00
|
|
|
virtual void run() override;
|
2019-10-01 12:10:34 -05:00
|
|
|
void MacroDefined(const Token& MacroNameTok, const MacroDirective* MD) override;
|
2019-09-30 02:16:36 -05:00
|
|
|
void MacroUndefined(const Token& MacroNameTok, const MacroDefinition& MD,
|
2019-10-01 12:10:34 -05:00
|
|
|
const MacroDirective* Undef) override;
|
2019-09-30 02:16:36 -05:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
isPPCallback = true
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
clang::Preprocessor& m_rPP;
|
|
|
|
std::unordered_map<std::string, Entry> m_aDefMap;
|
|
|
|
};
|
|
|
|
|
2019-11-16 08:39:12 -06:00
|
|
|
DuplicateDefines::DuplicateDefines(const loplugin::InstantiationData& data)
|
2019-09-30 02:16:36 -05:00
|
|
|
: Plugin(data)
|
|
|
|
, m_rPP(compiler.getPreprocessor())
|
|
|
|
{
|
|
|
|
compiler.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DuplicateDefines::run()
|
|
|
|
{
|
|
|
|
// nothing, only check preprocessor usage
|
|
|
|
}
|
|
|
|
|
|
|
|
void DuplicateDefines::MacroDefined(const Token& rMacroNameTok, const MacroDirective*)
|
|
|
|
{
|
|
|
|
auto aLoc = rMacroNameTok.getLocation();
|
|
|
|
if (ignoreLocation(aLoc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string aMacroName = m_rPP.getSpelling(rMacroNameTok);
|
|
|
|
|
|
|
|
// some testing macro
|
|
|
|
if (aMacroName == "RTL_STRING_CONST_FUNCTION")
|
|
|
|
return;
|
|
|
|
if (aMacroName == "rtl")
|
|
|
|
return;
|
2019-12-18 06:28:58 -06:00
|
|
|
// we replicate these macros in all the .hrc files
|
|
|
|
if (aMacroName == "NC_" || aMacroName == "NNC_")
|
2019-09-30 02:16:36 -05:00
|
|
|
return;
|
2020-02-03 09:10:59 -06:00
|
|
|
// We define this prior to including <windows.h>:
|
|
|
|
if (aMacroName == "WIN32_LEAN_AND_MEAN")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-09-30 02:16:36 -05:00
|
|
|
// TODO no obvious fix for these
|
|
|
|
if (aMacroName == "FID_SEARCH_NOW" || aMacroName == "FID_SVX_START" || aMacroName == "FN_PARAM")
|
|
|
|
return;
|
2019-12-18 06:28:58 -06:00
|
|
|
// ignore for now, requires adding too many includes to sw/
|
|
|
|
if (aMacroName == "MM50")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// ignore for now, we have the same define in svx and sw, but I can't remove one of them because
|
|
|
|
// they reference strings in different resource bundles
|
|
|
|
if (aMacroName == "STR_UNDO_COL_DELETE" || aMacroName == "STR_UNDO_ROW_DELETE"
|
|
|
|
|| aMacroName == "STR_TABLE_NUMFORMAT" || aMacroName == "STR_DELETE")
|
|
|
|
return;
|
2019-09-30 02:16:36 -05:00
|
|
|
|
2020-01-17 11:38:37 -06:00
|
|
|
if (m_aDefMap.emplace(aMacroName, Entry{ aLoc }).second)
|
2019-09-30 02:16:36 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-01-17 11:38:37 -06:00
|
|
|
|
|
|
|
// Happens e.g. with macros defined in include/premac.h, which is intended to be included
|
|
|
|
// (without include guards) multiple times:
|
|
|
|
auto const other = m_aDefMap[aMacroName].m_aLoc;
|
|
|
|
assert(aLoc == compiler.getSourceManager().getSpellingLoc(aLoc));
|
|
|
|
assert(other == compiler.getSourceManager().getSpellingLoc(other));
|
|
|
|
if ((compiler.getSourceManager().getFilename(aLoc)
|
|
|
|
== compiler.getSourceManager().getFilename(other))
|
|
|
|
&& (compiler.getSourceManager().getSpellingLineNumber(aLoc)
|
|
|
|
== compiler.getSourceManager().getSpellingLineNumber(other))
|
|
|
|
&& (compiler.getSourceManager().getSpellingColumnNumber(aLoc)
|
|
|
|
== compiler.getSourceManager().getSpellingColumnNumber(other)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
report(DiagnosticsEngine::Warning, "duplicate defines", aLoc);
|
|
|
|
report(DiagnosticsEngine::Note, "previous define", other);
|
2019-09-30 02:16:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void DuplicateDefines::MacroUndefined(const Token& rMacroNameTok, const MacroDefinition& /*MD*/,
|
|
|
|
const MacroDirective* /*Undef*/)
|
|
|
|
{
|
|
|
|
auto aLoc = rMacroNameTok.getLocation();
|
|
|
|
if (ignoreLocation(aLoc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string aMacroName = m_rPP.getSpelling(rMacroNameTok);
|
|
|
|
m_aDefMap.erase(aMacroName);
|
|
|
|
}
|
|
|
|
|
2019-12-18 06:28:58 -06:00
|
|
|
loplugin::Plugin::Registration<DuplicateDefines> X("duplicatedefines", true);
|
2019-09-30 02:16:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|