Bug 1438866 - Mark empty filenames not interesting. r?emilio
In some cases we have SourceLocation objections that are isMacroId()
rather than isFileId() and so don't have a filename at all. In other
cases the filename is something clang-internal like "<scratch>". In
both of these situations we don't want to output any analysis data.
However, the code previously was taking the empty filename and resolving
it relative to the source folder, and then tried to write to the
source folder or create a file corresponding to a subfolder. This
resulted in general badness. This patch makes sure we ignore analysis
from places that don't have a corresponding file so that we don't
have this problem.
MozReview-Commit-ID: 7WluygY9Uy8
--- a/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp
+++ b/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp
@@ -95,17 +95,21 @@ struct FileInfo {
// We're in the objdir, so we are probably a generated header
// We use the escape character to indicate the objdir nature.
// Note that output also has the `/' already placed
Interesting = true;
Realname.replace(0, Objdir.length(), GENERATED);
return;
}
- Interesting = Rname.compare(0, Srcdir.length(), Srcdir) == 0;
+ // Empty filenames can get turned into Srcdir when they are resolved as
+ // absolute paths, so we should exclude files that are exactly equal to
+ // Srcdir or anything outside Srcdir.
+ Interesting = (Rname.length() > Srcdir.length()) &&
+ (Rname.compare(0, Srcdir.length(), Srcdir) == 0);
if (Interesting) {
// Remove the trailing `/' as well.
Realname.erase(0, Srcdir.length() + 1);
}
}
std::string Realname;
std::vector<std::string> Output;
bool Interesting;
@@ -171,19 +175,26 @@ private:
FileID Id = SM.getFileID(Loc);
std::map<FileID, std::unique_ptr<FileInfo>>::iterator It;
It = FileMap.find(Id);
if (It == FileMap.end()) {
// We haven't seen this file before. We need to make the FileInfo
// structure information ourselves
std::string Filename = SM.getFilename(Loc);
- std::string Absolute = getAbsolutePath(Filename);
- if (Absolute.empty()) {
- Absolute = Filename;
+ std::string Absolute;
+ // If Loc is a macro id rather than a file id, it Filename might be
+ // empty. Also for some types of file locations that are clang-internal
+ // like "<scratch>" it can return an empty Filename. In these cases we
+ // want to leave Absolute as empty.
+ if (!Filename.empty()) {
+ Absolute = getAbsolutePath(Filename);
+ if (Absolute.empty()) {
+ Absolute = Filename;
+ }
}
std::unique_ptr<FileInfo> Info = llvm::make_unique<FileInfo>(Absolute);
It = FileMap.insert(std::make_pair(Id, std::move(Info))).first;
}
return It->second.get();
}
// Helpers for processing declarations