llvm.org GIT mirror llvm / 1508c82
SourceMgr: make valid buffer IDs start from one Use 0 for the invalid buffer instead of -1/~0 and switch to unsigned representation to enable more idiomatic usage. Also introduce a trivial SourceMgr::getMainFileID() instead of hard-coding 0/1 to identify the main file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212398 91177308-0d34-0410-b5e6-96231b3b80d8 Alp Toker 5 years ago
10 changed file(s) with 73 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
6565 DiagHandlerTy DiagHandler;
6666 void *DiagContext;
6767
68 bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); }
69
6870 SourceMgr(const SourceMgr&) LLVM_DELETED_FUNCTION;
6971 void operator=(const SourceMgr&) LLVM_DELETED_FUNCTION;
7072 public:
8789 void *getDiagContext() const { return DiagContext; }
8890
8991 const SrcBuffer &getBufferInfo(unsigned i) const {
90 assert(i < Buffers.size() && "Invalid Buffer ID!");
91 return Buffers[i];
92 assert(isValidBufferID(i));
93 return Buffers[i - 1];
9294 }
9395
9496 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
95 assert(i < Buffers.size() && "Invalid Buffer ID!");
96 return Buffers[i].Buffer;
97 assert(isValidBufferID(i));
98 return Buffers[i - 1].Buffer;
9799 }
98100
99101 size_t getNumBuffers() const {
100102 return Buffers.size();
101103 }
102104
105 unsigned getMainFileID() const {
106 assert(getNumBuffers());
107 return 1;
108 }
109
103110 SMLoc getParentIncludeLoc(unsigned i) const {
104 assert(i < Buffers.size() && "Invalid Buffer ID!");
105 return Buffers[i].IncludeLoc;
111 assert(isValidBufferID(i));
112 return Buffers[i - 1].IncludeLoc;
106113 }
107114
108115 /// Add a new source buffer to this source manager. This takes ownership of
112119 NB.Buffer = F;
113120 NB.IncludeLoc = IncludeLoc;
114121 Buffers.push_back(NB);
115 return Buffers.size() - 1;
122 return Buffers.size();
116123 }
117124
118125 /// Search for a file with the specified name in the current directory or in
119126 /// one of the IncludeDirs.
120127 ///
121 /// If no file is found, this returns ~0, otherwise it returns the buffer ID
128 /// If no file is found, this returns 0, otherwise it returns the buffer ID
122129 /// of the stacked file. The full path to the included file can be found in
123130 /// \p IncludedFile.
124131 size_t AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc,
126133
127134 /// Return the ID of the buffer containing the specified location.
128135 ///
129 /// -1 is returned if the buffer is not found.
130 int FindBufferContainingLoc(SMLoc Loc) const;
136 /// 0 is returned if the buffer is not found.
137 unsigned FindBufferContainingLoc(SMLoc Loc) const;
131138
132139 /// Find the line number for the specified location in the specified file.
133140 /// This is not a fast method.
134 unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const {
141 unsigned FindLineNumber(SMLoc Loc, unsigned BufferID = 0) const {
135142 return getLineAndColumn(Loc, BufferID).first;
136143 }
137144
138145 /// Find the line and column number for the specified location in the
139146 /// specified file. This is not a fast method.
140 std::pair
141 getLineAndColumn(SMLoc Loc, int BufferID = -1) const;
147 std::pair getLineAndColumn(SMLoc Loc,
148 unsigned BufferID = 0) const;
142149
143150 /// Emit a message about the specified location with the specified string.
144151 ///
4646 SecureLog = nullptr;
4747 SecureLogUsed = false;
4848
49 if (SrcMgr && SrcMgr->getNumBuffers() > 0)
50 MainFileName = SrcMgr->getMemoryBuffer(0)->getBufferIdentifier();
49 if (SrcMgr && SrcMgr->getNumBuffers())
50 MainFileName =
51 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
5152 }
5253
5354 MCContext::~MCContext() {
927927
928928 // Finding the line number is the expensive part which is why we just don't
929929 // pass it in as for some symbols we won't create a dwarf label.
930 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
930 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
931931 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
932932
933933 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
132132
133133 /// This is the current buffer index we're lexing from as managed by the
134134 /// SourceMgr object.
135 int CurBuffer;
135 unsigned CurBuffer;
136136
137137 AsmCond TheCondState;
138138 std::vector TheCondStack;
161161 StringRef CppHashFilename;
162162 int64_t CppHashLineNumber;
163163 SMLoc CppHashLoc;
164 int CppHashBuf;
164 unsigned CppHashBuf;
165165 /// When generating dwarf for assembly source files we need to calculate the
166166 /// logical line number based on the last parsed cpp hash file line comment
167167 /// and current line. Since this is slow and messes up the SourceMgr's
168168 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
169169 SMLoc LastQueryIDLoc;
170 int LastQueryBuffer;
170 unsigned LastQueryBuffer;
171171 unsigned LastQueryLine;
172172
173173 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
309309 /// current token is not set; clients should ensure Lex() is called
310310 /// subsequently.
311311 ///
312 /// \param InBuffer If not -1, should be the known buffer id that contains the
312 /// \param InBuffer If not 0, should be the known buffer id that contains the
313313 /// location.
314 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
314 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
315315
316316 /// \brief Parse up to the end of statement and a return the contents from the
317317 /// current token until the end of the statement; the current token on exit
490490 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
491491 const MCAsmInfo &_MAI)
492492 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
493 PlatformParser(nullptr), CurBuffer(0), MacrosEnabledFlag(true),
494 HadError(false), CppHashLineNumber(0), AssemblerDialect(~0U),
495 IsDarwin(false), ParsingInlineAsm(false) {
493 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
494 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
495 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
496496 // Save the old handler.
497497 SavedDiagHandler = SrcMgr.getDiagHandler();
498498 SavedDiagContext = SrcMgr.getDiagContext();
565565
566566 bool AsmParser::enterIncludeFile(const std::string &Filename) {
567567 std::string IncludedFile;
568 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
569 if (NewBuf == -1)
568 unsigned NewBuf =
569 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
570 if (!NewBuf)
570571 return true;
571572
572573 CurBuffer = NewBuf;
573
574574 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
575
576575 return false;
577576 }
578577
581580 /// returns true on failure.
582581 bool AsmParser::processIncbinFile(const std::string &Filename) {
583582 std::string IncludedFile;
584 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
585 if (NewBuf == -1)
583 unsigned NewBuf =
584 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
585 if (!NewBuf)
586586 return true;
587587
588588 // Pick up the bytes from the file and emit them.
590590 return false;
591591 }
592592
593 void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
594 if (InBuffer != -1) {
595 CurBuffer = InBuffer;
596 } else {
597 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
598 }
593 void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
594 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
599595 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
600596 }
601597
16961692
16971693 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
16981694 const SMLoc &DiagLoc = Diag.getLoc();
1699 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1700 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1695 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1696 unsigned CppHashBuf =
1697 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
17011698
17021699 // Like SourceMgr::printMessage() we need to print the include stack if any
17031700 // before printing the message.
1704 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1705 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
1701 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1702 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1703 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
17061704 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
17071705 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
17081706 }
45814579 // Build the IR assembly string.
45824580 std::string AsmStringIR;
45834581 raw_string_ostream OS(AsmStringIR);
4584 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4585 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4582 StringRef ASMString =
4583 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4584 const char *AsmStart = ASMString.begin();
4585 const char *AsmEnd = ASMString.end();
45864586 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
45874587 for (const AsmRewrite &AR : AsmStrRewrites) {
45884588 AsmRewriteKind Kind = AR.Kind;
649649 }
650650
651651 // Write the message.
652 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
652 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
653653 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
654654 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
655655 << LogMessage + "\n";
2626
2727 namespace {
2828 struct LineNoCacheTy {
29 int LastQueryBufferID;
29 unsigned LastQueryBufferID;
3030 const char *LastQuery;
3131 unsigned LineNoOfQuery;
3232 };
6161 MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
6262 }
6363
64 if (!NewBuf) return ~0U;
64 if (!NewBuf)
65 return 0;
6566
6667 return AddNewSourceBuffer(NewBuf.release(), IncludeLoc);
6768 }
6869
69
70 int SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
70 unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
7171 for (unsigned i = 0, e = Buffers.size(); i != e; ++i)
7272 if (Loc.getPointer() >= Buffers[i].Buffer->getBufferStart() &&
7373 // Use <= here so that a pointer to the null at the end of the buffer
7474 // is included as part of the buffer.
7575 Loc.getPointer() <= Buffers[i].Buffer->getBufferEnd())
76 return i;
77 return -1;
76 return i + 1;
77 return 0;
7878 }
7979
8080 std::pair
81 SourceMgr::getLineAndColumn(SMLoc Loc, int BufferID) const {
82 if (BufferID == -1) BufferID = FindBufferContainingLoc(Loc);
83 assert(BufferID != -1 && "Invalid Location!");
81 SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const {
82 if (!BufferID)
83 BufferID = FindBufferContainingLoc(Loc);
84 assert(BufferID && "Invalid Location!");
8485
8586 const MemoryBuffer *Buff = getMemoryBuffer(BufferID);
8687
124125 void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const {
125126 if (IncludeLoc == SMLoc()) return; // Top of stack.
126127
127 int CurBuf = FindBufferContainingLoc(IncludeLoc);
128 assert(CurBuf != -1 && "Invalid or unspecified location!");
128 unsigned CurBuf = FindBufferContainingLoc(IncludeLoc);
129 assert(CurBuf && "Invalid or unspecified location!");
129130
130131 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
131132
148149 std::string LineStr;
149150
150151 if (Loc.isValid()) {
151 int CurBuf = FindBufferContainingLoc(Loc);
152 assert(CurBuf != -1 && "Invalid or unspecified location!");
152 unsigned CurBuf = FindBufferContainingLoc(Loc);
153 assert(CurBuf && "Invalid or unspecified location!");
153154
154155 const MemoryBuffer *CurMB = getMemoryBuffer(CurBuf);
155156 BufferID = CurMB->getBufferIdentifier();
207208 }
208209
209210 if (Diagnostic.getLoc().isValid()) {
210 int CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
211 assert(CurBuf != -1 && "Invalid or unspecified location!");
211 unsigned CurBuf = FindBufferContainingLoc(Diagnostic.getLoc());
212 assert(CurBuf && "Invalid or unspecified location!");
212213 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
213214 }
214215
2626 using namespace llvm;
2727
2828 TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
29 CurBuffer = 0;
29 CurBuffer = SrcMgr.getMainFileID();
3030 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
3131 CurPtr = CurBuf->getBufferStart();
3232 TokStart = nullptr;
303303
304304 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
305305 IncludedFile);
306 if (CurBuffer == -1) {
306 if (!CurBuffer) {
307307 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
308308 return true;
309309 }
7272
7373 /// CurBuffer - This is the current buffer index we're lexing from as managed
7474 /// by the SourceMgr object.
75 int CurBuffer;
75 unsigned CurBuffer;
7676
7777 public:
7878 typedef std::map DependenciesMapTy;
241241 tool_output_file *Out) {
242242
243243 AsmLexer Lexer(MAI);
244 Lexer.setBuffer(SrcMgr.getMemoryBuffer(0));
244 Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID()));
245245
246246 bool Error = false;
247247 while (Lexer.Lex().isNot(AsmToken::Eof)) {
3636 : Id(&Name), Loc(Location) {}
3737 int operator<(const Tag &B) const { return *Id < *B.Id; }
3838 void emit(raw_ostream &OS) const {
39 int BufferID = SrcMgr.FindBufferContainingLoc(Loc);
40 const MemoryBuffer *CurMB = SrcMgr.getMemoryBuffer(BufferID);
39 const MemoryBuffer *CurMB =
40 SrcMgr.getMemoryBuffer(SrcMgr.FindBufferContainingLoc(Loc));
4141 const char *BufferName = CurMB->getBufferIdentifier();
4242 std::pair LineAndColumn = SrcMgr.getLineAndColumn(Loc);
4343 OS << *Id << "\t" << BufferName << "\t" << LineAndColumn.first << "\n";