aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorNathan Sidwell <nathan@acm.org>2021-11-08 09:45:26 -0800
committerNathan Sidwell <nathan@acm.org>2021-11-09 07:31:03 -0800
commitbf6986d99eaa68ad850eb335cb3f98fe6406ccd0 (patch)
tree96cb0e9c7de5cc72a0c7f3b7a8bdda8411730c6f /clang/lib/Basic/SourceManager.cpp
parentd510fd2bed030ff2c42e1a365e1898ac6e822694 (diff)
downloadllvm-bf6986d99eaa68ad850eb335cb3f98fe6406ccd0.zip
llvm-bf6986d99eaa68ad850eb335cb3f98fe6406ccd0.tar.gz
llvm-bf6986d99eaa68ad850eb335cb3f98fe6406ccd0.tar.bz2
[clang] GCC directive extension extension: Hash NNN lines
Some time back I extended GCC's '# NNN' line marker semantics. Specifically popping to a blank filename will restore the filename to that of the popped-to include. Restore to line 5 of including file (escaped BOL #'s to avoid git eliding them): \# 5 "" 2 Added documentation for this line control extension. This was useful in developing modules tests, but turned out to also be useful with machine-generated source code. Specifically, a generated include file that itself includes fragments from elsewhere. The ability to pop to the generated include file -- with its full path prefix -- is useful for diagnostic & debug purposes. For instance something like: // Machine generated -- DO NOT EDIT Type Var = { \# 7 "encoded.dsl" 1 // push to snippet-container {snippet, of, code} \# 6 " 2 // Restore to machined-generated source , }; // user-code ... \#include "dsl.h" ... That pop to "" will restore the filename to '..includepath../dsl.h', which is better than restoring to plain "dsl.h". Differential Revision: https://reviews.llvm.org/D113425
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp34
1 files changed, 18 insertions, 16 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 8cba379..c2e7b68 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -207,28 +207,30 @@ void LineTableInfo::AddLineNote(FileID FID, unsigned Offset, unsigned LineNo,
SrcMgr::CharacteristicKind FileKind) {
std::vector<LineEntry> &Entries = LineEntries[FID];
- // An unspecified FilenameID means use the last filename if available, or the
- // main source file otherwise.
- if (FilenameID == -1 && !Entries.empty())
- FilenameID = Entries.back().FilenameID;
-
assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
"Adding line entries out of order!");
unsigned IncludeOffset = 0;
- if (EntryExit == 0) { // No #include stack change.
- IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
- } else if (EntryExit == 1) {
+ if (EntryExit == 1) {
+ // Push #include
IncludeOffset = Offset-1;
- } else if (EntryExit == 2) {
- assert(!Entries.empty() && Entries.back().IncludeOffset &&
- "PPDirectives should have caught case when popping empty include stack");
-
- // Get the include loc of the last entries' include loc as our include loc.
- IncludeOffset = 0;
- if (const LineEntry *PrevEntry =
- FindNearestLineEntry(FID, Entries.back().IncludeOffset))
+ } else {
+ const auto *PrevEntry = Entries.empty() ? nullptr : &Entries.back();
+ if (EntryExit == 2) {
+ // Pop #include
+ assert(PrevEntry && PrevEntry->IncludeOffset &&
+ "PPDirectives should have caught case when popping empty include "
+ "stack");
+ PrevEntry = FindNearestLineEntry(FID, PrevEntry->IncludeOffset);
+ }
+ if (PrevEntry) {
IncludeOffset = PrevEntry->IncludeOffset;
+ if (FilenameID == -1) {
+ // An unspecified FilenameID means use the previous (or containing)
+ // filename if available, or the main source file otherwise.
+ FilenameID = PrevEntry->FilenameID;
+ }
+ }
}
Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,