Skip to content

Commit e08464f

Browse files
committed
Avoid including FileManager.h from SourceManager.h
Most clients of SourceManager.h need to do things like turning source locations into file & line number pairs, but this doesn't require bringing in FileManager.h and LLVM's FS headers. The main code change here is to sink SM::createFileID into the cpp file. I reason that this is not performance critical because it doesn't happen on the diagnostic path, it happens along the paths of macro expansion (could be hot) and new includes (less hot). Saves some includes: 309 - /usr/local/google/home/rnk/llvm-project/clang/include/clang/Basic/FileManager.h 272 - /usr/local/google/home/rnk/llvm-project/clang/include/clang/Basic/FileSystemOptions.h 271 - /usr/local/google/home/rnk/llvm-project/llvm/include/llvm/Support/VirtualFileSystem.h 267 - /usr/local/google/home/rnk/llvm-project/llvm/include/llvm/Support/FileSystem.h 266 - /usr/local/google/home/rnk/llvm-project/llvm/include/llvm/Support/Chrono.h Differential Revision: https://reviews.llvm.org/D75406
1 parent 526a4f2 commit e08464f

37 files changed

+166
-71
lines changed

clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "HeaderMapCollector.h"
1111
#include "PathConfig.h"
1212
#include "SymbolInfo.h"
13+
#include "clang/Basic/FileManager.h"
1314
#include "clang/Basic/IdentifierTable.h"
1415
#include "clang/Basic/SourceManager.h"
1516
#include "clang/Lex/MacroInfo.h"

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ExpandModularHeadersPPCallbacks.h"
10+
#include "clang/Basic/FileManager.h"
1011
#include "clang/Frontend/CompilerInstance.h"
1112
#include "clang/Lex/PreprocessorOptions.h"
1213
#include "clang/Serialization/ASTReader.h"

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include "clang/Lex/Preprocessor.h"
1414
#include "llvm/ADT/DenseSet.h"
1515

16+
namespace llvm {
17+
namespace vfs {
18+
class OverlayFileSystem;
19+
class InMemoryFileSystem;
20+
} // namespace vfs
21+
} // namespace llvm
22+
1623
namespace clang {
1724
class CompilerInstance;
1825

clang-tools-extra/clangd/Format.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Format.h"
99
#include "Logger.h"
1010
#include "clang/Basic/SourceManager.h"
11+
#include "clang/Basic/FileManager.h"
1112
#include "clang/Format/Format.h"
1213
#include "clang/Lex/Lexer.h"
1314
#include "clang/Tooling/Core/Replacement.h"

clang-tools-extra/pp-trace/PPCallbacksTracker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
#include "PPCallbacksTracker.h"
17+
#include "clang/Basic/FileManager.h"
1718
#include "clang/Lex/MacroArgs.h"
1819
#include "llvm/Support/raw_ostream.h"
1920

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
#include "clang/AST/DeclFriend.h"
5353
#include "clang/AST/DeclObjC.h"
5454
#include "clang/AST/DeclTemplate.h"
55-
#include "clang/AST/ParentMapContext.h"
5655
#include "clang/AST/Expr.h"
5756
#include "clang/AST/ExprCXX.h"
5857
#include "clang/AST/ExprObjC.h"
5958
#include "clang/AST/LambdaCapture.h"
6059
#include "clang/AST/NestedNameSpecifier.h"
6160
#include "clang/AST/OpenMPClause.h"
6261
#include "clang/AST/OperationKinds.h"
62+
#include "clang/AST/ParentMapContext.h"
6363
#include "clang/AST/Stmt.h"
6464
#include "clang/AST/StmtCXX.h"
6565
#include "clang/AST/StmtObjC.h"
@@ -72,6 +72,7 @@
7272
#include "clang/ASTMatchers/ASTMatchersMacros.h"
7373
#include "clang/Basic/AttrKinds.h"
7474
#include "clang/Basic/ExceptionSpecificationType.h"
75+
#include "clang/Basic/FileManager.h"
7576
#include "clang/Basic/IdentifierTable.h"
7677
#include "clang/Basic/LLVM.h"
7778
#include "clang/Basic/SourceManager.h"

clang/include/clang/Basic/SourceManager.h

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#define LLVM_CLANG_BASIC_SOURCEMANAGER_H
3636

3737
#include "clang/Basic/Diagnostic.h"
38-
#include "clang/Basic/FileManager.h"
3938
#include "clang/Basic/SourceLocation.h"
4039
#include "llvm/ADT/ArrayRef.h"
4140
#include "llvm/ADT/BitVector.h"
@@ -60,6 +59,9 @@ namespace clang {
6059

6160
class ASTReader;
6261
class ASTWriter;
62+
class FileManager;
63+
class FileEntry;
64+
class FileEntryRef;
6365
class LineTableInfo;
6466
class SourceManager;
6567

@@ -830,24 +832,11 @@ class SourceManager : public RefCountedBase<SourceManager> {
830832
/// This translates NULL into standard input.
831833
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
832834
SrcMgr::CharacteristicKind FileCharacter,
833-
int LoadedID = 0, unsigned LoadedOffset = 0) {
834-
assert(SourceFile && "Null source file!");
835-
const SrcMgr::ContentCache *IR =
836-
getOrCreateContentCache(SourceFile, isSystem(FileCharacter));
837-
assert(IR && "getOrCreateContentCache() cannot return NULL");
838-
return createFileID(IR, SourceFile->getName(), IncludePos, FileCharacter,
839-
LoadedID, LoadedOffset);
840-
}
835+
int LoadedID = 0, unsigned LoadedOffset = 0);
841836

842837
FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos,
843838
SrcMgr::CharacteristicKind FileCharacter,
844-
int LoadedID = 0, unsigned LoadedOffset = 0) {
845-
const SrcMgr::ContentCache *IR = getOrCreateContentCache(
846-
&SourceFile.getFileEntry(), isSystem(FileCharacter));
847-
assert(IR && "getOrCreateContentCache() cannot return NULL");
848-
return createFileID(IR, SourceFile.getName(), IncludePos, FileCharacter,
849-
LoadedID, LoadedOffset);
850-
}
839+
int LoadedID = 0, unsigned LoadedOffset = 0);
851840

852841
/// Create a new FileID that represents the specified memory buffer.
853842
///
@@ -856,12 +845,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
856845
FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
857846
SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
858847
int LoadedID = 0, unsigned LoadedOffset = 0,
859-
SourceLocation IncludeLoc = SourceLocation()) {
860-
StringRef Name = Buffer->getBufferIdentifier();
861-
return createFileID(
862-
createMemBufferContentCache(Buffer.release(), /*DoNotFree*/ false),
863-
Name, IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
864-
}
848+
SourceLocation IncludeLoc = SourceLocation());
865849

866850
enum UnownedTag { Unowned };
867851

@@ -872,20 +856,12 @@ class SourceManager : public RefCountedBase<SourceManager> {
872856
FileID createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer,
873857
SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
874858
int LoadedID = 0, unsigned LoadedOffset = 0,
875-
SourceLocation IncludeLoc = SourceLocation()) {
876-
return createFileID(createMemBufferContentCache(Buffer, /*DoNotFree*/ true),
877-
Buffer->getBufferIdentifier(), IncludeLoc,
878-
FileCharacter, LoadedID, LoadedOffset);
879-
}
859+
SourceLocation IncludeLoc = SourceLocation());
880860

881861
/// Get the FileID for \p SourceFile if it exists. Otherwise, create a
882862
/// new FileID for the \p SourceFile.
883863
FileID getOrCreateFileID(const FileEntry *SourceFile,
884-
SrcMgr::CharacteristicKind FileCharacter) {
885-
FileID ID = translateFile(SourceFile);
886-
return ID.isValid() ? ID : createFileID(SourceFile, SourceLocation(),
887-
FileCharacter);
888-
}
864+
SrcMgr::CharacteristicKind FileCharacter);
889865

890866
/// Return a new SourceLocation that encodes the
891867
/// fact that a token from SpellingLoc should actually be referenced from
@@ -1025,17 +1001,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
10251001
}
10261002

10271003
/// Returns the FileEntryRef for the provided FileID.
1028-
Optional<FileEntryRef> getFileEntryRefForID(FileID FID) const {
1029-
bool Invalid = false;
1030-
const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1031-
if (Invalid || !Entry.isFile())
1032-
return None;
1033-
1034-
const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
1035-
if (!Content || !Content->OrigEntry)
1036-
return None;
1037-
return FileEntryRef(Entry.getFile().getName(), *Content->OrigEntry);
1038-
}
1004+
Optional<FileEntryRef> getFileEntryRefForID(FileID FID) const;
10391005

10401006
/// Returns the FileEntry record for the provided SLocEntry.
10411007
const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
@@ -1098,11 +1064,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
10981064
}
10991065

11001066
/// Return the filename of the file containing a SourceLocation.
1101-
StringRef getFilename(SourceLocation SpellingLoc) const {
1102-
if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
1103-
return F->getName();
1104-
return StringRef();
1105-
}
1067+
StringRef getFilename(SourceLocation SpellingLoc) const;
11061068

11071069
/// Return the source location corresponding to the first byte of
11081070
/// the specified file.

clang/include/clang/Frontend/CompilerInstance.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,7 @@ class CompilerInstance : public ModuleLoader {
390390
/// @name Virtual File System
391391
/// {
392392

393-
llvm::vfs::FileSystem &getVirtualFileSystem() const {
394-
return getFileManager().getVirtualFileSystem();
395-
}
393+
llvm::vfs::FileSystem &getVirtualFileSystem() const;
396394

397395
/// }
398396
/// @name File Manager

clang/include/clang/Frontend/VerifyDiagnosticConsumer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_FRONTEND_VERIFYDIAGNOSTICCONSUMER_H
1111

1212
#include "clang/Basic/Diagnostic.h"
13+
#include "clang/Basic/FileManager.h"
1314
#include "clang/Basic/LLVM.h"
1415
#include "clang/Basic/SourceLocation.h"
1516
#include "clang/Lex/Preprocessor.h"

clang/include/clang/Lex/DirectoryLookup.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
#define LLVM_CLANG_LEX_DIRECTORYLOOKUP_H
1515

1616
#include "clang/Basic/LLVM.h"
17+
#include "clang/Basic/FileManager.h"
1718
#include "clang/Basic/SourceManager.h"
1819
#include "clang/Lex/ModuleMap.h"
1920

2021
namespace clang {
2122
class HeaderMap;
22-
class DirectoryEntry;
23-
class FileEntry;
2423
class HeaderSearch;
2524
class Module;
2625

0 commit comments

Comments
 (0)