Skip to content

Commit 5d30887

Browse files
committed
Fix crash and disable nfs check on windows
1 parent 529aac1 commit 5d30887

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,14 @@ class SourceManager : public RefCountedBase<SourceManager> {
18541854
SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const;
18551855

18561856
/// Retrieve the name of a file suitable for diagnostics.
1857-
StringRef getNameForDiagnostic(StringRef Filename) const;
1857+
// FIXME: Passing in the DiagnosticOptions here is a workaround for the
1858+
// fact that installapi does some weird things with DiagnosticsEngines,
1859+
// which causes the 'Diag' member of SourceManager (or at least the
1860+
// DiagnosticsOptions member thereof) to be a dangling reference
1861+
// sometimes. We should probably fix that or decouple the two classes
1862+
// to avoid this issue entirely.
1863+
StringRef getNameForDiagnostic(StringRef Filename,
1864+
const DiagnosticOptions &Opts) const;
18581865

18591866
private:
18601867
friend class ASTReader;

clang/lib/Basic/SourceManager.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,19 +2404,36 @@ SourceManagerForFile::SourceManagerForFile(StringRef FileName,
24042404
SourceMgr->setMainFileID(ID);
24052405
}
24062406

2407-
StringRef SourceManager::getNameForDiagnostic(StringRef Filename) const {
2407+
StringRef
2408+
SourceManager::getNameForDiagnostic(StringRef Filename,
2409+
const DiagnosticOptions &Opts) const {
24082410
OptionalFileEntryRef File = getFileManager().getOptionalFileRef(Filename);
24092411
if (!File)
24102412
return Filename;
24112413

2412-
// Try to simplify paths that contain '..' in any case since paths to
2413-
// standard library headers especially tend to get quite long otherwise.
2414-
// Only do that for local filesystems though to avoid slowing down
2415-
// compilation too much.
2416-
bool Absolute = Diag.getDiagnosticOptions().AbsolutePath;
2417-
bool AlwaysSimplify = File->getName().contains("..") &&
2418-
llvm::sys::fs::is_local(File->getName());
2419-
if (!Absolute && !AlwaysSimplify)
2414+
bool SimplifyPath = [&] {
2415+
if (Opts.AbsolutePath)
2416+
return true;
2417+
2418+
// Try to simplify paths that contain '..' in any case since paths to
2419+
// standard library headers especially tend to get quite long otherwise.
2420+
// Only do that for local filesystems though to avoid slowing down
2421+
// compilation too much.
2422+
if (!File->getName().contains(".."))
2423+
return false;
2424+
2425+
// If we're not on Windows, check if we're on a network file system and
2426+
// avoid simplifying the path in that case since that can be slow. On
2427+
// Windows, the check for a local filesystem is already slow, so skip it.
2428+
#ifndef _WIN32
2429+
if (!llvm::sys::fs::is_local(File->getName()))
2430+
return false;
2431+
#endif
2432+
2433+
return true;
2434+
}();
2435+
2436+
if (!SimplifyPath)
24202437
return Filename;
24212438

24222439
// This may involve computing canonical names, so cache the result.
@@ -2451,7 +2468,7 @@ StringRef SourceManager::getNameForDiagnostic(StringRef Filename) const {
24512468
// In some cases, the resolved path may actually end up being longer (e.g.
24522469
// if it was originally a relative path), so just retain whichever one
24532470
// ends up being shorter.
2454-
if (!Absolute && TempBuf.size() > Filename.size())
2471+
if (!Opts.AbsolutePath && TempBuf.size() > Filename.size())
24552472
CacheEntry = Filename;
24562473
else
24572474
CacheEntry = TempBuf.str().copy(DiagNameAlloc);

clang/lib/Frontend/SARIFDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ SARIFDiagnostic::addDiagnosticLevelToRule(SarifRule Rule,
163163

164164
llvm::StringRef SARIFDiagnostic::emitFilename(StringRef Filename,
165165
const SourceManager &SM) {
166-
return SM.getNameForDiagnostic(Filename);
166+
return SM.getNameForDiagnostic(Filename, DiagOpts);
167167
}
168168

169169
/// Print out the file/line/column information and include trace.

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
738738
}
739739

740740
void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) {
741-
OS << SM.getNameForDiagnostic(Filename);
741+
OS << SM.getNameForDiagnostic(Filename, DiagOpts);
742742
}
743743

744744
/// Print out the file/line/column information and include trace.

clang/test/Frontend/absolute-paths.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include "absolute-paths.h"
1010

11-
// Check whether the diagnostic from the header above includes the dummy
12-
// directory in the path.
13-
// NORMAL: SystemHeaderPrefix
11+
// Check that the bogus prefix we added is stripped out even if absolute paths
12+
// are disabled.
13+
// NORMAL-NOT: SystemHeaderPrefix
1414
// ABSOLUTE-NOT: SystemHeaderPrefix
1515
// CHECK: warning: non-void function does not return a value
1616

0 commit comments

Comments
 (0)