diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2016-09-27 10:00:03 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2016-09-27 12:08:52 +0000 |
commit | 3468dab9ff02ecdf5d0806a00e15b9b579c8dc35 (patch) | |
tree | da1d37473e9d477ff814f1775c145e6e1a65f442 | |
parent | 7c3cb23136229c748984b49847af6f729ce3e6ba (diff) |
clang plugins: do "dotdot" normalisation
which fixes some false positives
Change-Id: I555349180b5ca819f29695789f1545ba2177bd09
Reviewed-on: https://gerrit.libreoffice.org/29320
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | compilerplugins/clang/constantparam.cxx | 1 | ||||
-rw-r--r-- | compilerplugins/clang/countusersofdefaultparams.cxx | 1 | ||||
-rw-r--r-- | compilerplugins/clang/plugin.cxx | 61 | ||||
-rw-r--r-- | compilerplugins/clang/plugin.hxx | 2 | ||||
-rw-r--r-- | compilerplugins/clang/singlevalfields.cxx | 1 | ||||
-rw-r--r-- | compilerplugins/clang/unuseddefaultparams.cxx | 1 | ||||
-rw-r--r-- | compilerplugins/clang/unusedenumvalues.cxx | 1 | ||||
-rw-r--r-- | compilerplugins/clang/unusedfields.cxx | 1 | ||||
-rw-r--r-- | compilerplugins/clang/unusedmethods.cxx | 1 |
9 files changed, 42 insertions, 28 deletions
diff --git a/compilerplugins/clang/constantparam.cxx b/compilerplugins/clang/constantparam.cxx index 61428ac60dff..c517e59739c3 100644 --- a/compilerplugins/clang/constantparam.cxx +++ b/compilerplugins/clang/constantparam.cxx @@ -137,6 +137,7 @@ void ConstantParam::addToCallSet(const FunctionDecl* functionDecl, int paramInde aInfo.callValue = callValue; aInfo.sourceLocation = filename.str() + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + normalizeDotDotInFilePath(aInfo.sourceLocation); callSet.insert(aInfo); } diff --git a/compilerplugins/clang/countusersofdefaultparams.cxx b/compilerplugins/clang/countusersofdefaultparams.cxx index a425cfcb89cc..b6323d054bd0 100644 --- a/compilerplugins/clang/countusersofdefaultparams.cxx +++ b/compilerplugins/clang/countusersofdefaultparams.cxx @@ -130,6 +130,7 @@ void CountUsersOfDefaultParams::niceName(const FunctionDecl* functionDecl, MyFun } aInfo.sourceLocation = locationToString(functionDecl->getLocation()); + normalizeDotDotInFilePath(aInfo.sourceLocation); } bool CountUsersOfDefaultParams::VisitCallExpr(const CallExpr * callExpr) { diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx index 9f13ee7441b9..3507371c0ab5 100644 --- a/compilerplugins/clang/plugin.cxx +++ b/compilerplugins/clang/plugin.cxx @@ -55,34 +55,7 @@ bool Plugin::ignoreLocation( SourceLocation loc ) return true; } std::string s(bufferName); - for (std::string::size_type i = 0;;) { - i = s.find("/.", i); - if (i == std::string::npos) { - break; - } - if (i + 2 == s.length() || s[i + 2] == '/') { - s.erase(i, 2); // [AAA]/.[/CCC] -> [AAA][/CCC] - } else if (s[i + 2] == '.' - && (i + 3 == s.length() || s[i + 3] == '/')) - { - if (i == 0) { // /..[/CCC] -> /..[/CCC] - break; - } - auto j = s.rfind('/', i - 1); - if (j == std::string::npos) { - // BBB/..[/CCC] -> BBB/..[/CCC] (instead of BBB/../CCC -> - // CCC, to avoid wrong ../../CCC -> CCC; relative paths - // shouldn't happen anyway, and even if they did, wouldn't - // match against WORKDIR anyway, as WORKDIR should be - // absolute): - break; - } - s.erase(j, i + 3 - j); // AAA/BBB/..[/CCC] -> AAA[/CCC] - i = j; - } else { - i += 2; - } - } + normalizeDotDotInFilePath(s); if (strncmp(s.c_str(), WORKDIR, strlen(WORKDIR)) == 0) { return true; } @@ -93,6 +66,38 @@ bool Plugin::ignoreLocation( SourceLocation loc ) return true; } +void Plugin::normalizeDotDotInFilePath( std::string & s ) + { + for (std::string::size_type i = 0;;) { + i = s.find("/.", i); + if (i == std::string::npos) { + break; + } + if (i + 2 == s.length() || s[i + 2] == '/') { + s.erase(i, 2); // [AAA]/.[/CCC] -> [AAA][/CCC] + } else if (s[i + 2] == '.' + && (i + 3 == s.length() || s[i + 3] == '/')) + { + if (i == 0) { // /..[/CCC] -> /..[/CCC] + break; + } + auto j = s.rfind('/', i - 1); + if (j == std::string::npos) { + // BBB/..[/CCC] -> BBB/..[/CCC] (instead of BBB/../CCC -> + // CCC, to avoid wrong ../../CCC -> CCC; relative paths + // shouldn't happen anyway, and even if they did, wouldn't + // match against WORKDIR anyway, as WORKDIR should be + // absolute): + break; + } + s.erase(j, i + 3 - j); // AAA/BBB/..[/CCC] -> AAA[/CCC] + i = j; + } else { + i += 2; + } + } + } + void Plugin::registerPlugin( Plugin* (*create)( const InstantiationData& ), const char* optionName, bool isPPCallback, bool byDefault ) { PluginHandler::registerPlugin( create, optionName, isPPCallback, byDefault ); diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx index 6b9f682082b4..635dcfe4c0d3 100644 --- a/compilerplugins/clang/plugin.hxx +++ b/compilerplugins/clang/plugin.hxx @@ -75,6 +75,8 @@ class Plugin which is not allowed to be changed. */ bool isInUnoIncludeFile(SourceLocation spellingLocation) const; + + static void normalizeDotDotInFilePath(std::string&); private: static void registerPlugin( Plugin* (*create)( const InstantiationData& ), const char* optionName, bool isPPCallback, bool byDefault ); template< typename T > static Plugin* createHelper( const InstantiationData& data ); diff --git a/compilerplugins/clang/singlevalfields.cxx b/compilerplugins/clang/singlevalfields.cxx index c663bd6d5d96..491d260d6d31 100644 --- a/compilerplugins/clang/singlevalfields.cxx +++ b/compilerplugins/clang/singlevalfields.cxx @@ -116,6 +116,7 @@ void SingleValFields::niceName(const FieldDecl* fieldDecl, MyFieldInfo& aInfo) SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( fieldDecl->getLocation() ); StringRef name = compiler.getSourceManager().getFilename(expansionLoc); aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + normalizeDotDotInFilePath(aInfo.sourceLocation); } bool SingleValFields::VisitFieldDecl( const FieldDecl* fieldDecl ) diff --git a/compilerplugins/clang/unuseddefaultparams.cxx b/compilerplugins/clang/unuseddefaultparams.cxx index 11cd37da7e31..6b5aefa3cfdd 100644 --- a/compilerplugins/clang/unuseddefaultparams.cxx +++ b/compilerplugins/clang/unuseddefaultparams.cxx @@ -124,6 +124,7 @@ MyFuncInfo UnusedDefaultParams::niceName(const FunctionDecl* functionDecl) SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( functionDecl->getLocation() ); StringRef name = compiler.getSourceManager().getFilename(expansionLoc); aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + normalizeDotDotInFilePath(aInfo.sourceLocation); return aInfo; } diff --git a/compilerplugins/clang/unusedenumvalues.cxx b/compilerplugins/clang/unusedenumvalues.cxx index 6e3c87402a00..bfde9608cce2 100644 --- a/compilerplugins/clang/unusedenumvalues.cxx +++ b/compilerplugins/clang/unusedenumvalues.cxx @@ -101,6 +101,7 @@ MyEnumValueInfo UnusedEnumValues::niceName(const EnumConstantDecl* enumDecl) SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( enumDecl->getLocation() ); StringRef name = compiler.getSourceManager().getFilename(expansionLoc); aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + normalizeDotDotInFilePath(aInfo.sourceLocation); return aInfo; } diff --git a/compilerplugins/clang/unusedfields.cxx b/compilerplugins/clang/unusedfields.cxx index 41fab03b8d06..6e4da40439e1 100644 --- a/compilerplugins/clang/unusedfields.cxx +++ b/compilerplugins/clang/unusedfields.cxx @@ -109,6 +109,7 @@ MyFieldInfo UnusedFields::niceName(const FieldDecl* fieldDecl) SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( fieldDecl->getLocation() ); StringRef name = compiler.getSourceManager().getFilename(expansionLoc); aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + normalizeDotDotInFilePath(aInfo.sourceLocation); return aInfo; } diff --git a/compilerplugins/clang/unusedmethods.cxx b/compilerplugins/clang/unusedmethods.cxx index c16b446e5648..3c5498d79651 100644 --- a/compilerplugins/clang/unusedmethods.cxx +++ b/compilerplugins/clang/unusedmethods.cxx @@ -166,6 +166,7 @@ MyFuncInfo UnusedMethods::niceName(const FunctionDecl* functionDecl) SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( functionDecl->getLocation() ); StringRef name = compiler.getSourceManager().getFilename(expansionLoc); aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + normalizeDotDotInFilePath(aInfo.sourceLocation); return aInfo; } |