Drop the leading CWD prefix from source file paths if present
On some mac machines, the CWD is prefixed to the source file
path, and then the plugin can't match path names relative to the
source root. This drops the CWD so that we get a consistent view
of paths being always relative to the CWD.
[email protected], [email protected]
Bug: 41497066, 336028803
Change-Id: I52a99ed0b568969e44f44ae246f9a994e04f4777
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5473364
Commit-Queue: danakj <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1291458}
diff --git a/tools/clang/plugins/UnsafeBuffersPlugin.cpp b/tools/clang/plugins/UnsafeBuffersPlugin.cpp
index 644664a3..2fe72a64 100644
--- a/tools/clang/plugins/UnsafeBuffersPlugin.cpp
+++ b/tools/clang/plugins/UnsafeBuffersPlugin.cpp
@@ -216,8 +216,20 @@
return cache_it->second;
}
- // Drop the ../ prefixes.
llvm::StringRef cmp_filename = filename;
+
+ // If the path is absolute, drop the prefix up to the current working
+ // directory. Some mac machines are passing absolute paths to source files,
+ // but it's the absolute path to the build directory (the current working
+ // directory here) then a relative path from there.
+ llvm::SmallVector<char> cwd;
+ if (llvm::sys::fs::current_path(cwd).value() == 0) {
+ if (cmp_filename.consume_front(llvm::StringRef(cwd.data(), cwd.size()))) {
+ cmp_filename.consume_front("/");
+ }
+ }
+
+ // Drop the ../ prefixes.
while (cmp_filename.consume_front("./") ||
cmp_filename.consume_front("../"))
;