better solution for ignoreLocation for tree-wide plugins

Change-Id: I7336003e038781d4ef50380fa49f66b5ff19379f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135589
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2022-06-10 13:11:13 +02:00
parent 5dd9aeb825
commit d0f61d94d6
14 changed files with 42 additions and 84 deletions

View File

@ -61,6 +61,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
// ignore some files that make clang crash inside EvaluateAsInt
std::string fn(handler.getMainFileName());
loplugin::normalizeDotDotInFilePath(fn);

View File

@ -65,6 +65,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes

View File

@ -47,6 +47,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes

View File

@ -56,6 +56,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes

View File

@ -73,6 +73,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes

View File

@ -230,16 +230,21 @@ bool PluginHandler::ignoreLocation(SourceLocation loc) {
bool PluginHandler::checkIgnoreLocation(SourceLocation loc)
{
// If a location comes from a PCH, it is not necessary to check it
// in every compilation using the PCH, since with Clang we use
// -building-pch-with-obj to build a separate precompiled_foo.cxx file
// for the PCH, and so it is known that everything in the PCH will
// be checked while compiling this file. Skip the checks for all
// other files using the PCH.
if( !compiler.getSourceManager().isLocalSourceLocation( loc ))
// The tree-wide analysis plugins (like unusedmethods) don't want
// this logic, they only want to ignore externl code
if (!treeWideAnalysisMode)
{
if( !compiler.getLangOpts().BuildingPCHWithObjectFile )
return true;
// If a location comes from a PCH, it is not necessary to check it
// in every compilation using the PCH, since with Clang we use
// -building-pch-with-obj to build a separate precompiled_foo.cxx file
// for the PCH, and so it is known that everything in the PCH will
// be checked while compiling this file. Skip the checks for all
// other files using the PCH.
if( !compiler.getSourceManager().isLocalSourceLocation( loc ))
{
if( !compiler.getLangOpts().BuildingPCHWithObjectFile )
return true;
}
}
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))

View File

@ -68,6 +68,8 @@ public:
// Is all code that could see `decl` defined in this TU?
bool isAllRelevantCodeDefined(NamedDecl const * decl);
void enableTreeWideAnalysisMode() { treeWideAnalysisMode = true; }
private:
void handleOption( const std::string& option );
void createPlugins( std::set< std::string > rewriters );
@ -81,6 +83,8 @@ private:
std::string warningsOnly;
bool warningsAsErrors;
bool debugMode = false;
//// Used by the tree-wide analysis plugins like unusedmethods, etc.
bool treeWideAnalysisMode = false;
std::vector<std::pair<char const*, char const*>> mvModifiedRanges;
// Used internally by isAllRelevantCodeDefined:

View File

@ -94,6 +94,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
if (!isUnitTestMode())

View File

@ -62,6 +62,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes

View File

@ -67,6 +67,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
if (!isUnitTestMode())

View File

@ -163,8 +163,6 @@ public:
private:
MyFieldInfo niceName(const FieldDecl*);
bool ignoreLocation(SourceLocation loc);
bool checkIgnoreLocation(SourceLocation loc);
void checkTouchedFromOutside(const FieldDecl* fieldDecl, const Expr* memberExpr);
void checkIfReadFrom(const FieldDecl* fieldDecl, const Expr* memberExpr);
void checkIfWrittenTo(const FieldDecl* fieldDecl, const Expr* memberExpr);
@ -184,6 +182,8 @@ private:
void UnusedFields::run()
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
if (!isUnitTestMode())
@ -281,42 +281,6 @@ MyFieldInfo UnusedFields::niceName(const FieldDecl* fieldDecl)
return aInfo;
}
/**
* Our need to see everything conflicts with the PCH code in pluginhandler::ignoreLocation,
* so we have to do this ourselves.
*/
bool UnusedFields::ignoreLocation(SourceLocation loc)
{
static std::unordered_map<SourceLocation, bool> checkedMap;
auto it = checkedMap.find(loc);
if (it != checkedMap.end())
return it->second;
bool ignore = checkIgnoreLocation(loc);
checkedMap.emplace(loc, ignore);
return ignore;
}
bool UnusedFields::checkIgnoreLocation(SourceLocation loc)
{
// simplified form of the code in PluginHandler::checkIgnoreLocation
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))
return true;
PresumedLoc presumedLoc = compiler.getSourceManager().getPresumedLoc( expansionLoc );
if( presumedLoc.isInvalid())
return true;
const char* bufferName = presumedLoc.getFilename();
if (bufferName == NULL
|| loplugin::hasPathnamePrefix(bufferName, SRCDIR "/external/")
|| loplugin::hasPathnamePrefix(bufferName, WORKDIR "/"))
return true;
if( loplugin::hasPathnamePrefix(bufferName, BUILDDIR "/")
|| loplugin::hasPathnamePrefix(bufferName, SRCDIR "/") )
return false; // ok
return true;
}
bool UnusedFields::VisitFieldDecl( const FieldDecl* fieldDecl )
{
fieldDecl = fieldDecl->getCanonicalDecl();

View File

@ -84,6 +84,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
StringRef fn(handler.getMainFileName());
// ignore external code, makes this run faster
if (fn.contains("UnpackedTarball"))
@ -133,8 +135,6 @@ private:
MyFuncInfo niceName(const FunctionDecl* functionDecl);
std::string toString(SourceLocation loc);
void functionTouchedFromExpr( const FunctionDecl* calleeFunctionDecl, const Expr* expr );
bool ignoreLocation(SourceLocation loc);
bool checkIgnoreLocation(SourceLocation loc);
CXXRecordDecl const * currentCxxRecordDecl = nullptr;
FunctionDecl const * currentFunctionDecl = nullptr;
@ -197,41 +197,6 @@ MyFuncInfo UnusedMethods::niceName(const FunctionDecl* functionDecl)
return aInfo;
}
/**
* Our need to see everything conflicts with the PCH code in pluginhandler::ignoreLocation,
* so we have to do this ourselves.
*/
bool UnusedMethods::ignoreLocation(SourceLocation loc)
{
static std::unordered_map<SourceLocation, bool> checkedMap;
auto it = checkedMap.find(loc);
if (it != checkedMap.end())
return it->second;
bool ignore = checkIgnoreLocation(loc);
checkedMap.emplace(loc, ignore);
return ignore;
}
bool UnusedMethods::checkIgnoreLocation(SourceLocation loc)
{
// simplified form of the code in PluginHandler::checkIgnoreLocation
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))
return true;
PresumedLoc presumedLoc = compiler.getSourceManager().getPresumedLoc( expansionLoc );
if( presumedLoc.isInvalid())
return true;
const char* bufferName = presumedLoc.getFilename();
if (bufferName == NULL
|| loplugin::hasPathnamePrefix(bufferName, SRCDIR "/external/")
|| loplugin::hasPathnamePrefix(bufferName, WORKDIR "/"))
return true;
if( loplugin::hasPathnamePrefix(bufferName, BUILDDIR "/")
|| loplugin::hasPathnamePrefix(bufferName, SRCDIR "/") )
return false; // ok
return true;
}
std::string UnusedMethods::toString(SourceLocation loc)
{
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );

View File

@ -175,6 +175,8 @@ private:
void UnusedVarsGlobal::run()
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
if (!isUnitTestMode())

View File

@ -68,6 +68,8 @@ public:
virtual void run() override
{
handler.enableTreeWideAnalysisMode();
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes