Skip to content

Commit fa13d21

Browse files
committed
[clang][cas] Move IncludeFile after IncludeTree in the header NFC
Moves IncludeFile after IncludeTree in the header file in preparation for changing it to IncludeTree::File. Also moves FileInfo to IncludeTree to simplify declaration. (cherry picked from commit f36469e)
1 parent 3221056 commit fa13d21

File tree

3 files changed

+85
-79
lines changed

3 files changed

+85
-79
lines changed

clang/include/clang/CAS/IncludeTree.h

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -45,68 +45,7 @@ template <typename NodeT> class IncludeTreeBase : public ObjectProxy {
4545
friend class IncludeTreeRoot;
4646
};
4747

48-
/// Represents a \p SourceManager file (or buffer in the case of preprocessor
49-
/// predefines) that got included by the preprocessor.
50-
class IncludeFile : public IncludeTreeBase<IncludeFile> {
51-
public:
52-
static constexpr StringRef getNodeKind() { return "File"; }
53-
54-
ObjectRef getFilenameRef() const { return getReference(0); }
55-
ObjectRef getContentsRef() const { return getReference(1); }
56-
57-
Expected<ObjectProxy> getFilename() {
58-
return getCAS().getProxy(getFilenameRef());
59-
}
60-
61-
Expected<ObjectProxy> getContents() {
62-
return getCAS().getProxy(getContentsRef());
63-
}
64-
65-
struct FileInfo {
66-
StringRef Filename;
67-
StringRef Contents;
68-
};
69-
70-
Expected<FileInfo> getFileInfo() {
71-
auto Filename = getFilename();
72-
if (!Filename)
73-
return Filename.takeError();
74-
auto Contents = getContents();
75-
if (!Contents)
76-
return Contents.takeError();
77-
return FileInfo{Filename->getData(), Contents->getData()};
78-
}
79-
80-
static Expected<IncludeFile> create(ObjectStore &DB, StringRef Filename,
81-
ObjectRef Contents);
82-
83-
llvm::Error print(llvm::raw_ostream &OS, unsigned Indent = 0);
84-
85-
static bool isValid(const ObjectProxy &Node) {
86-
if (!IncludeTreeBase::isValid(Node))
87-
return false;
88-
IncludeTreeBase Base(Node);
89-
return Base.getNumReferences() == 2 && Base.getData().empty();
90-
}
91-
static bool isValid(ObjectStore &DB, ObjectRef Ref) {
92-
auto Node = DB.getProxy(Ref);
93-
if (!Node) {
94-
llvm::consumeError(Node.takeError());
95-
return false;
96-
}
97-
return isValid(*Node);
98-
}
99-
100-
private:
101-
friend class IncludeTreeBase<IncludeFile>;
102-
friend class IncludeFileList;
103-
friend class IncludeTree;
104-
friend class IncludeTreeRoot;
105-
106-
explicit IncludeFile(ObjectProxy Node) : IncludeTreeBase(std::move(Node)) {
107-
assert(isValid(*this));
108-
}
109-
};
48+
class IncludeFile;
11049

11150
/// Represents a DAG of included files by the preprocessor.
11251
/// Each node in the DAG represents a particular inclusion of a file that
@@ -117,22 +56,17 @@ class IncludeTree : public IncludeTreeBase<IncludeTree> {
11756
public:
11857
static constexpr StringRef getNodeKind() { return "Tree"; }
11958

120-
Expected<IncludeFile> getBaseFile() {
121-
auto Node = getCAS().getProxy(getBaseFileRef());
122-
if (!Node)
123-
return Node.takeError();
124-
return IncludeFile(std::move(*Node));
125-
}
59+
Expected<IncludeFile> getBaseFile();
12660

12761
/// The include file that resulted in this include-tree.
12862
ObjectRef getBaseFileRef() const { return getReference(0); }
12963

130-
Expected<IncludeFile::FileInfo> getBaseFileInfo() {
131-
auto File = getBaseFile();
132-
if (!File)
133-
return File.takeError();
134-
return File->getFileInfo();
135-
}
64+
struct FileInfo {
65+
StringRef Filename;
66+
StringRef Contents;
67+
};
68+
69+
Expected<FileInfo> getBaseFileInfo();
13670

13771
SrcMgr::CharacteristicKind getFileCharacteristic() const {
13872
return (SrcMgr::CharacteristicKind)dataSkippingIncludes().front();
@@ -216,6 +150,64 @@ class IncludeTree : public IncludeTreeBase<IncludeTree> {
216150
}
217151
};
218152

153+
/// Represents a \p SourceManager file (or buffer in the case of preprocessor
154+
/// predefines) that got included by the preprocessor.
155+
class IncludeFile : public IncludeTreeBase<IncludeFile> {
156+
public:
157+
static constexpr StringRef getNodeKind() { return "File"; }
158+
159+
ObjectRef getFilenameRef() const { return getReference(0); }
160+
ObjectRef getContentsRef() const { return getReference(1); }
161+
162+
Expected<ObjectProxy> getFilename() {
163+
return getCAS().getProxy(getFilenameRef());
164+
}
165+
166+
Expected<ObjectProxy> getContents() {
167+
return getCAS().getProxy(getContentsRef());
168+
}
169+
170+
Expected<IncludeTree::FileInfo> getFileInfo() {
171+
auto Filename = getFilename();
172+
if (!Filename)
173+
return Filename.takeError();
174+
auto Contents = getContents();
175+
if (!Contents)
176+
return Contents.takeError();
177+
return IncludeTree::FileInfo{Filename->getData(), Contents->getData()};
178+
}
179+
180+
static Expected<IncludeFile> create(ObjectStore &DB, StringRef Filename,
181+
ObjectRef Contents);
182+
183+
llvm::Error print(llvm::raw_ostream &OS, unsigned Indent = 0);
184+
185+
static bool isValid(const ObjectProxy &Node) {
186+
if (!IncludeTreeBase::isValid(Node))
187+
return false;
188+
IncludeTreeBase Base(Node);
189+
return Base.getNumReferences() == 2 && Base.getData().empty();
190+
}
191+
static bool isValid(ObjectStore &DB, ObjectRef Ref) {
192+
auto Node = DB.getProxy(Ref);
193+
if (!Node) {
194+
llvm::consumeError(Node.takeError());
195+
return false;
196+
}
197+
return isValid(*Node);
198+
}
199+
200+
private:
201+
friend class IncludeTreeBase<IncludeFile>;
202+
friend class IncludeFileList;
203+
friend class IncludeTree;
204+
friend class IncludeTreeRoot;
205+
206+
explicit IncludeFile(ObjectProxy Node) : IncludeTreeBase(std::move(Node)) {
207+
assert(isValid(*this));
208+
}
209+
};
210+
219211
/// A flat list of \p IncludeFile entries. This is used along with a simple
220212
/// implementation of a \p vfs::FileSystem produced via
221213
/// \p createIncludeTreeFileSystem().

clang/lib/CAS/IncludeTree.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ Expected<IncludeFile> IncludeFile::create(ObjectStore &DB, StringRef Filename,
4242
return IncludeTreeBase::create(DB, Refs, {});
4343
}
4444

45+
Expected<IncludeFile> IncludeTree::getBaseFile() {
46+
auto Node = getCAS().getProxy(getBaseFileRef());
47+
if (!Node)
48+
return Node.takeError();
49+
return IncludeFile(std::move(*Node));
50+
}
51+
52+
Expected<IncludeTree::FileInfo> IncludeTree::getBaseFileInfo() {
53+
auto File = getBaseFile();
54+
if (!File)
55+
return File.takeError();
56+
return File->getFileInfo();
57+
}
58+
4559
llvm::Error IncludeTree::forEachInclude(
4660
llvm::function_ref<llvm::Error(std::pair<IncludeTree, uint32_t>)>
4761
Callback) {

clang/unittests/CAS/IncludeTreeTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ TEST(IncludeTree, IncludeTreeScan) {
7171
ASSERT_THAT_ERROR(Main->getBaseFile().moveInto(MainFile),
7272
llvm::Succeeded());
7373
EXPECT_EQ(Main->getFileCharacteristic(), SrcMgr::C_User);
74-
IncludeFile::FileInfo FI;
74+
IncludeTree::FileInfo FI;
7575
ASSERT_THAT_ERROR(MainFile->getFileInfo().moveInto(FI), llvm::Succeeded());
7676
EXPECT_EQ(FI.Filename, "t.cpp");
7777
EXPECT_EQ(FI.Contents, MainContents);
@@ -83,7 +83,7 @@ TEST(IncludeTree, IncludeTreeScan) {
8383
EXPECT_EQ(Main->getIncludeOffset(0), uint32_t(0));
8484
{
8585
EXPECT_EQ(Predef->getFileCharacteristic(), SrcMgr::C_User);
86-
IncludeFile::FileInfo FI;
86+
IncludeTree::FileInfo FI;
8787
ASSERT_THAT_ERROR(Predef->getBaseFileInfo().moveInto(FI),
8888
llvm::Succeeded());
8989
EXPECT_EQ(FI.Filename, "<built-in>");
@@ -95,7 +95,7 @@ TEST(IncludeTree, IncludeTreeScan) {
9595
{
9696
ASSERT_THAT_ERROR(A1->getBaseFile().moveInto(A1File), llvm::Succeeded());
9797
EXPECT_EQ(A1->getFileCharacteristic(), SrcMgr::C_User);
98-
IncludeFile::FileInfo FI;
98+
IncludeTree::FileInfo FI;
9999
ASSERT_THAT_ERROR(A1File->getFileInfo().moveInto(FI), llvm::Succeeded());
100100
EXPECT_EQ(FI.Filename, "./a1.h");
101101
EXPECT_EQ(FI.Contents, A1Contents);
@@ -109,7 +109,7 @@ TEST(IncludeTree, IncludeTreeScan) {
109109
{
110110
ASSERT_THAT_ERROR(B1->getBaseFile().moveInto(B1File), llvm::Succeeded());
111111
EXPECT_EQ(B1->getFileCharacteristic(), SrcMgr::C_User);
112-
IncludeFile::FileInfo FI;
112+
IncludeTree::FileInfo FI;
113113
ASSERT_THAT_ERROR(B1->getBaseFileInfo().moveInto(FI), llvm::Succeeded());
114114
EXPECT_EQ(FI.Filename, "./b1.h");
115115
EXPECT_EQ(FI.Contents, "");
@@ -124,7 +124,7 @@ TEST(IncludeTree, IncludeTreeScan) {
124124
{
125125
ASSERT_THAT_ERROR(Sys->getBaseFile().moveInto(SysFile), llvm::Succeeded());
126126
EXPECT_EQ(Sys->getFileCharacteristic(), SrcMgr::C_System);
127-
IncludeFile::FileInfo FI;
127+
IncludeTree::FileInfo FI;
128128
ASSERT_THAT_ERROR(Sys->getBaseFileInfo().moveInto(FI), llvm::Succeeded());
129129
EXPECT_EQ(FI.Filename, "sys/sys.h");
130130
EXPECT_EQ(FI.Contents, "");

0 commit comments

Comments
 (0)