Kaydet (Commit) 3468dab9 authored tarafından Noel Grandin's avatar Noel Grandin

clang plugins: do "dotdot" normalisation

which fixes some false positives

Change-Id: I555349180b5ca819f29695789f1545ba2177bd09
Reviewed-on: https://gerrit.libreoffice.org/29320Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
Tested-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 7c3cb231
......@@ -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);
}
......
......@@ -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) {
......
......@@ -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 );
......
......@@ -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 );
......
......@@ -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 )
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment