llvm.org GIT mirror llvm / 7ee5d5f
move include searching logic from TGLexer to SourceMgr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73845 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
6 changed file(s) with 40 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
5656 /// Buffers - This is all of the buffers that we are reading from.
5757 std::vector Buffers;
5858
59 // IncludeDirectories - This is the list of directories we should search for
60 // include files in.
61 std::vector IncludeDirectories;
62
5963 SourceMgr(const SourceMgr&); // DO NOT IMPLEMENT
6064 void operator=(const SourceMgr&); // DO NOT IMPLEMENT
6165 public:
6266 SourceMgr() {}
6367 ~SourceMgr();
68
69 void setIncludeDirs(const std::vector &Dirs) {
70 IncludeDirectories = Dirs;
71 }
6472
6573 const SrcBuffer &getBufferInfo(unsigned i) const {
6674 assert(i < Buffers.size() && "Invalid Buffer ID!");
8593 return Buffers.size()-1;
8694 }
8795
96 /// AddIncludeFile - Search for a file with the specified name in the current
97 /// directory or in one of the IncludeDirs. If no file is found, this returns
98 /// ~0, otherwise it returns the buffer ID of the stacked file.
99 unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc);
100
88101 /// FindBufferContainingLoc - Return the ID of the buffer containing the
89102 /// specified location, returning -1 if not found.
90103 int FindBufferContainingLoc(SMLoc Loc) const;
2323 Buffers.pop_back();
2424 }
2525 }
26
27 /// AddIncludeFile - Search for a file with the specified name in the current
28 /// directory or in one of the IncludeDirs. If no file is found, this returns
29 /// ~0, otherwise it returns the buffer ID of the stacked file.
30 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
31 SMLoc IncludeLoc) {
32
33 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
34
35 // If the file didn't exist directly, see if it's in an include path.
36 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
37 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
38 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
39 }
40
41 if (NewBuf == 0) return ~0U;
42
43 return AddNewSourceBuffer(NewBuf, IncludeLoc);
44 }
45
2646
2747 /// FindBufferContainingLoc - Return the ID of the buffer containing the
2848 /// specified location, returning -1 if not found.
277277 // Get the string.
278278 std::string Filename = CurStrVal;
279279
280 // Try to find the file.
281 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
282
283 // If the file didn't exist directly, see if it's in an include path.
284 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
285 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
286 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
287 }
288
289 if (NewBuf == 0) {
280
281 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
282 if (CurBuffer == ~0U) {
290283 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
291284 return true;
292285 }
293286
294287 // Save the line number and lex buffer of the includer.
295 CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, SMLoc::getFromPointer(CurPtr));
296
297 CurBuf = NewBuf;
288 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
298289 CurPtr = CurBuf->getBufferStart();
299290 return false;
300291 }
7272 /// by the SourceMgr object.
7373 int CurBuffer;
7474
75 // IncludeDirectories - This is the list of directories we should search for
76 // include files in.
77 std::vector IncludeDirectories;
7875 public:
7976 TGLexer(SourceMgr &SrcMgr);
8077 ~TGLexer() {}
81
82 void setIncludeDirs(const std::vector &Dirs) {
83 IncludeDirectories = Dirs;
84 }
8578
8679 tgtok::TokKind Lex() {
8780 return CurCode = LexToken();
4848 public:
4949 TGParser(SourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
5050
51 void setIncludeDirs(const std::vector &D){Lex.setIncludeDirs(D);}
52
5351 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
5452 /// routines return true on error, or false on success.
5553 bool ParseFile();
145145
146146 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
147147 SrcMgr.AddNewSourceBuffer(F, SMLoc());
148
149 TGParser Parser(SrcMgr);
150148
151149 // Record the location of the include directory so that the lexer can find
152150 // it later.
153 Parser.setIncludeDirs(IncludeDirs);
151 SrcMgr.setIncludeDirs(IncludeDirs);
152
153 TGParser Parser(SrcMgr);
154154
155155 return Parser.ParseFile();
156156 }