summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/plugin.cxx
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-05-18 09:56:01 +0200
committerStephan Bergmann <sbergman@redhat.com>2017-05-18 09:56:01 +0200
commitdf8d092c3a3c65bc23bc3c1da281cc4cb10a1e3d (patch)
treeea5daefc9c1665ab14aeeedc0de4298c7c85a8b1 /compilerplugins/clang/plugin.cxx
parent33cbc99fea5a3e4b87e883fd60ec97d4503109f3 (diff)
Adapt pathname checks to mixed usage of \ and / on Windows
Change-Id: I91bc89a9076c6642e06b238f65f2d31a1d20c6b5
Diffstat (limited to 'compilerplugins/clang/plugin.cxx')
-rw-r--r--compilerplugins/clang/plugin.cxx77
1 files changed, 58 insertions, 19 deletions
diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx
index d5b2401c8bf9..9c308b1f8509 100644
--- a/compilerplugins/clang/plugin.cxx
+++ b/compilerplugins/clang/plugin.cxx
@@ -12,6 +12,7 @@
#include "plugin.hxx"
#include <cassert>
+#include <cstddef>
#include <string>
#include <clang/Basic/FileManager.h>
@@ -42,8 +43,8 @@ bool Plugin::ignoreLocation( SourceLocation loc )
return true;
const char* bufferName = compiler.getSourceManager().getPresumedLoc( expansionLoc ).getFilename();
if (bufferName == NULL
- || strncmp( bufferName, SRCDIR "/external/", strlen( SRCDIR "/external/" )) == 0
- || strcmp( bufferName, SRCDIR "/sdext/source/pdfimport/wrapper/keyword_list" ) == 0 )
+ || hasPathnamePrefix(bufferName, SRCDIR "/external/")
+ || isSamePathname(bufferName, SRCDIR "/sdext/source/pdfimport/wrapper/keyword_list") )
// workdir/CustomTarget/sdext/pdfimport/hash.cxx is generated from
// sdext/source/pdfimport/wrapper/keyword_list by gperf, which
// inserts various #line directives denoting the latter into the
@@ -55,7 +56,7 @@ bool Plugin::ignoreLocation( SourceLocation loc )
// generated into the start of hash.cxx, #if'ed for __GNUC__, but
// for clang-cl it is an issue)
return true;
- if( strncmp( bufferName, WORKDIR, strlen( WORKDIR )) == 0 )
+ if( hasPathnamePrefix(bufferName, WORKDIR) )
{
// workdir/CustomTarget/vcl/unx/kde4/tst_exclude_socket_notifiers.moc
// includes
@@ -67,12 +68,12 @@ bool Plugin::ignoreLocation( SourceLocation loc )
}
std::string s(bufferName);
normalizeDotDotInFilePath(s);
- if (strncmp(s.c_str(), WORKDIR, strlen(WORKDIR)) == 0) {
+ if (hasPathnamePrefix(s, WORKDIR)) {
return true;
}
}
- if( strncmp( bufferName, BUILDDIR, strlen( BUILDDIR )) == 0
- || strncmp( bufferName, SRCDIR, strlen( SRCDIR )) == 0 )
+ if( hasPathnamePrefix(bufferName, BUILDDIR)
+ || hasPathnamePrefix(bufferName, SRCDIR) )
return false; // ok
return true;
}
@@ -165,19 +166,19 @@ bool Plugin::isInUnoIncludeFile(SourceLocation spellingLocation) const {
StringRef name {
compiler.getSourceManager().getFilename(spellingLocation) };
return compiler.getSourceManager().isInMainFile(spellingLocation)
- ? (name == SRCDIR "/cppu/source/cppu/compat.cxx"
- || name == SRCDIR "/cppuhelper/source/compat.cxx"
- || name == SRCDIR "/sal/osl/all/compat.cxx")
- : (name.startswith(SRCDIR "/include/com/")
- || name.startswith(SRCDIR "/include/cppu/")
- || name.startswith(SRCDIR "/include/cppuhelper/")
- || name.startswith(SRCDIR "/include/osl/")
- || name.startswith(SRCDIR "/include/rtl/")
- || name.startswith(SRCDIR "/include/sal/")
- || name.startswith(SRCDIR "/include/salhelper/")
- || name.startswith(SRCDIR "/include/systools/")
- || name.startswith(SRCDIR "/include/typelib/")
- || name.startswith(SRCDIR "/include/uno/"));
+ ? (isSamePathname(name, SRCDIR "/cppu/source/cppu/compat.cxx")
+ || isSamePathname(name, SRCDIR "/cppuhelper/source/compat.cxx")
+ || isSamePathname(name, SRCDIR "/sal/osl/all/compat.cxx"))
+ : (hasPathnamePrefix(name, SRCDIR "/include/com/")
+ || hasPathnamePrefix(name, SRCDIR "/include/cppu/")
+ || hasPathnamePrefix(name, SRCDIR "/include/cppuhelper/")
+ || hasPathnamePrefix(name, SRCDIR "/include/osl/")
+ || hasPathnamePrefix(name, SRCDIR "/include/rtl/")
+ || hasPathnamePrefix(name, SRCDIR "/include/sal/")
+ || hasPathnamePrefix(name, SRCDIR "/include/salhelper/")
+ || hasPathnamePrefix(name, SRCDIR "/include/systools/")
+ || hasPathnamePrefix(name, SRCDIR "/include/typelib/")
+ || hasPathnamePrefix(name, SRCDIR "/include/uno/"));
}
bool Plugin::isInUnoIncludeFile(const FunctionDecl* functionDecl) const {
@@ -423,6 +424,44 @@ bool RewritePlugin::reportEditFailure( SourceLocation loc )
return false;
}
+namespace {
+
+template<typename Fn> bool checkPathname(
+ StringRef pathname, StringRef against, Fn check)
+{
+ if (check(pathname, against)) {
+ return true;
+ }
+#if defined _WIN32
+ for (std::size_t n = 0;;) {
+ std::size_t n1 = pathname.find('\\', n);
+ if (n1 >= against.size()) {
+ return check(pathname.substr(n), against.substr(n));
+ }
+ if (against[n1] != '/'
+ || pathname.substr(n, n1 - n) != against.substr(n, n1 - n))
+ {
+ break;
+ }
+ n = n1 + 1;
+ }
+#endif
+ return false;
+}
+
+}
+
+bool hasPathnamePrefix(StringRef pathname, StringRef prefix) {
+ return checkPathname(
+ pathname, prefix,
+ [](StringRef p, StringRef a) { return p.startswith(a); });
+}
+
+bool isSamePathname(StringRef pathname, StringRef other) {
+ return checkPathname(
+ pathname, other, [](StringRef p, StringRef a) { return p == a; });
+}
+
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */