loplugin:constantfunction

update the plugin similarly to
    commit 3ee3b36ae0
    Author: Stephan Bergmann <sbergman@redhat.com>
    Date:   Fri Oct 7 12:05:49 2016 +0200
    loplugin:staticmethods: Don't be fooled by decls starting with macros

Change-Id: I98ac3216d5acf89a49a26feb089ae2fd34e6e510
Reviewed-on: https://gerrit.libreoffice.org/29665
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2016-10-10 13:49:12 +02:00
parent fe82f6fc5c
commit 5c84f40ea2
41 changed files with 68 additions and 197 deletions

View File

@@ -21,50 +21,20 @@ namespace {
class ConstantFunction:
public RecursiveASTVisitor<ConstantFunction>, public loplugin::Plugin
{
StringRef getFilename(SourceLocation loc);
StringRef getFilename(const FunctionDecl* functionDecl);
public:
explicit ConstantFunction(InstantiationData const & data): Plugin(data) {}
void run() override
{
// these files crash clang-3.5 somewhere in the isEvaluatable/EvaluateAsXXX stuff
FileID mainFileID = compiler.getSourceManager().getMainFileID();
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "sc/source/core/data") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "sc/source/ui/app") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "sc/qa/unit") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "docuno.cxx") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "viewdata.cxx") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "calcoptionsdlg.cxx") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "sc/source/core/opencl") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "sc/source/core/tool") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "sc/source/core/tool") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getDir()->getName(), "desktop/source/lib") != 0) {
return;
}
/* FileID mainFileID = compiler.getSourceManager().getMainFileID();
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "bootstrapfixture.cxx") != 0) {
return;
}
if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "gtk3gtkinst.cxx") != 0) {
return;
}
}*/
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
@@ -72,9 +42,9 @@ public:
bool VisitFunctionDecl(const FunctionDecl *);
};
StringRef ConstantFunction::getFilename(SourceLocation loc)
StringRef ConstantFunction::getFilename(const FunctionDecl* functionDecl)
{
SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc);
SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(functionDecl->getCanonicalDecl()->getNameInfo().getLoc());
StringRef name { compiler.getSourceManager().getFilename(spellingLocation) };
return name;
}
@@ -90,12 +60,15 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
if (!pFunctionDecl->hasBody()) {
return true;
}
if (!pFunctionDecl->isThisDeclarationADefinition()) {
return true;
}
// stuff declared extern-C is almost always used as a some kind of callback
if (pFunctionDecl->isExternC()) {
return true;
}
StringRef aFileName = getFilename(pFunctionDecl->getLocStart());
StringRef aFileName = getFilename(pFunctionDecl);
// various tests in here are empty stubs under Linux
if (aFileName.startswith(SRCDIR "/sal/qa/")) {
@@ -128,7 +101,7 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
return true;
}
// salplug runtime-loading mechanism at work
if (getFilename(pFunctionDecl->getCanonicalDecl()->getLocStart()) == SRCDIR "/vcl/inc/salinst.hxx") {
if (aFileName == SRCDIR "/vcl/inc/salinst.hxx") {
return true;
}
// lots of callbacks here
@@ -144,7 +117,7 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
return true;
}
// used by code generated by python
if (getFilename(pFunctionDecl->getCanonicalDecl()->getLocStart()) == SRCDIR "/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx") {
if (aFileName == SRCDIR "/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx") {
return true;
}
// this test just test the include of some headers
@@ -178,7 +151,7 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
if (isa<CXXConstructorDecl>(pFunctionDecl) || isa<CXXDestructorDecl>(pFunctionDecl) || isa<CXXConversionDecl>(pFunctionDecl)) {
return true;
}
SourceLocation canonicalLoc = pFunctionDecl->getCanonicalDecl()->getLocStart();
SourceLocation canonicalLoc = pFunctionDecl->getCanonicalDecl()->getNameInfo().getLoc();
if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(canonicalLoc))) {
return true;
}
@@ -433,6 +406,28 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
if (aFunctionName == "clipboard_owner_init") {
return true;
}
// returns sizeof(struct) vcl/source/gdi/dibtools.cxx
if (aFunctionName == "getDIBV5HeaderSize") {
return true;
}
// windows only
if (aFunctionName == "InitAccessBridge") {
return true;
}
// callbacks
if (aFunctionName == "disabled_initSystray" || aFunctionName == "disabled_deInitSystray") {
return true;
}
// behind a BREAKPAD option
if (aFunctionName == "desktop::(anonymous namespace)::crashReportInfoExists") {
return true;
}
// LOK stuff
if (aFunctionName == "doc_getTileMode") {
return true;
}
std::string aImmediateMacro = "";
if (compiler.getSourceManager().isMacroBodyExpansion(pFunctionDecl->getLocStart()) ) {
@@ -456,6 +451,11 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
if (!pReturnStmt) {
return true;
}
if (const UnaryOperator* unaryOp = dyn_cast<UnaryOperator>(pReturnStmt->getRetValue())) {
if (unaryOp->getOpcode() == UO_AddrOf) {
return true;
}
}
if (pReturnStmt->getRetValue() != nullptr) {
// && !pReturnStmt->getRetValue()->isEvaluatable(compiler.getASTContext())) {
bool aBoolResult;
@@ -484,8 +484,13 @@ bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) {
DiagnosticsEngine::Warning,
aMessage,
pFunctionDecl->getLocStart())
<< pFunctionDecl->getSourceRange()
<< pFunctionDecl->getCanonicalDecl()->getSourceRange();
<< pFunctionDecl->getSourceRange();
if (pFunctionDecl != pFunctionDecl->getCanonicalDecl())
report(
DiagnosticsEngine::Note,
aMessage,
pFunctionDecl->getCanonicalDecl()->getLocStart())
<< pFunctionDecl->getCanonicalDecl()->getSourceRange();
return true;
}