Skip to content

Commit 05a65a9

Browse files
Remove Get_Source_Manager from PrettyPrint
The PrettyPrint class has some undesired global states dating back from when clang-extract was a closed-source single-file prototype. One of these is the use of `Get_Source_Manager`, which is abused in other parts of the software. Remove it. Signed-off-by: Giuliano Belinassi <gbelinassi@suse.de>
1 parent 076fc27 commit 05a65a9

File tree

5 files changed

+32
-38
lines changed

5 files changed

+32
-38
lines changed

libcextract/Error.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,15 @@ DiagsClass::DiagsClass(void)
3232
{}
3333

3434
/* Print error giving a piece of source code that caused the error. */
35-
void DiagsClass::EmitMessage(const StringRef message, DiagnosticsEngine::Level level, const SourceRange &range)
35+
void DiagsClass::EmitMessage(const StringRef message, DiagnosticsEngine::Level level,
36+
const SourceRange &range, const SourceManager &sm)
3637
{
37-
SourceManager *sm = PrettyPrint::Get_Source_Manager();
38-
if (sm) {
39-
CharSourceRange charsrc_range = CharSourceRange::getCharRange(range);
40-
FullSourceLoc loc = FullSourceLoc(range.getBegin(), *sm);
38+
CharSourceRange charsrc_range = CharSourceRange::getCharRange(range);
39+
FullSourceLoc loc = FullSourceLoc(range.getBegin(), sm);
4140

42-
const std::string ce_message = Append_CE(message);
43-
DiagsEngine.emitDiagnostic(loc, level, ce_message, charsrc_range, FixItHint(), nullptr);
44-
} else {
45-
EmitMessage(message, level);
46-
}
41+
const std::string ce_message = Append_CE(message);
42+
DiagsEngine.emitDiagnostic(loc, level, ce_message, charsrc_range, FixItHint(), nullptr);
4743
}
48-
4944
/* Print error without giving a piece of source code that caused the error. */
5045
void DiagsClass::EmitMessage(const StringRef message, DiagnosticsEngine::Level level)
5146
{

libcextract/Error.hh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DiagsClass
6565
DiagsClass(void);
6666

6767
void EmitMessage(const StringRef message, DiagnosticsEngine::Level level,
68-
const SourceRange &range);
68+
const SourceRange &range, const SourceManager &SM);
6969

7070
void EmitMessage(const StringRef message, DiagnosticsEngine::Level level);
7171

@@ -98,9 +98,10 @@ class DiagsClass
9898

9999
static inline void Emit_Message(const StringRef message,
100100
DiagnosticsEngine::Level level,
101-
const SourceRange &range)
101+
const SourceRange &range,
102+
const SourceManager &sm)
102103
{
103-
sDiag.EmitMessage(message, level, range);
104+
sDiag.EmitMessage(message, level, range, sm);
104105
}
105106

106107
static inline void Emit_Message(const StringRef message,
@@ -109,29 +110,35 @@ class DiagsClass
109110
sDiag.EmitMessage(message, level);
110111
}
111112

112-
static inline void Emit_Error(const StringRef message, const SourceRange &range)
113+
static inline void Emit_Error(const StringRef message,
114+
const SourceRange &range,
115+
const SourceManager &sm)
113116
{
114-
Emit_Message(message, DiagnosticsEngine::Level::Error, range);
117+
Emit_Message(message, DiagnosticsEngine::Level::Error, range, sm);
115118
}
116119

117120
static inline void Emit_Error(const StringRef message)
118121
{
119122
Emit_Message(message, DiagnosticsEngine::Level::Error);
120123
}
121124

122-
static inline void Emit_Warn(const StringRef message, const SourceRange &range)
125+
static inline void Emit_Warn(const StringRef message,
126+
const SourceRange &range,
127+
const SourceManager &sm)
123128
{
124-
Emit_Message(message, DiagnosticsEngine::Level::Warning, range);
129+
Emit_Message(message, DiagnosticsEngine::Level::Warning, range, sm);
125130
}
126131

127132
static inline void Emit_Warn(const StringRef message)
128133
{
129134
Emit_Message(message, DiagnosticsEngine::Level::Warning);
130135
}
131136

132-
static inline void Emit_Note(const StringRef message, const SourceRange &range)
137+
static inline void Emit_Note(const StringRef message,
138+
const SourceRange &range,
139+
const SourceManager &sm)
133140
{
134-
Emit_Message(message, DiagnosticsEngine::Level::Note, range);
141+
Emit_Message(message, DiagnosticsEngine::Level::Note, range, sm);
135142
}
136143

137144
static inline void Emit_Note(const StringRef message)

libcextract/IncludeTree.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ void IncludeTree::Build_Header_Tree(std::vector<std::string> const &must_expand)
7272
must_expand.end());
7373
MacroWalker mw(PP);
7474
bool already_seen_main = false;
75-
SourceManager *SM = PrettyPrint::Get_Source_Manager();
76-
OptionalFileEntryRef main = SM->getFileEntryRefForID(SM->getMainFileID());
75+
OptionalFileEntryRef main = SM.getFileEntryRefForID(SM.getMainFileID());
7776

7877
std::stack<IncludeNode *> stack;
7978
Root = new IncludeNode(this);
@@ -199,7 +198,7 @@ void IncludeTree::Build_Header_Map(void)
199198
if (warned == false) {
200199
const SourceRange &range = node->Get_Include_Spelling_Range();
201200
DiagsClass::Emit_Warn("project #include's the same file multiple times."
202-
" Only the first is registered.", range);
201+
" Only the first is registered.", range, SM);
203202
warned = true;
204203
}
205204
} else {
@@ -237,8 +236,7 @@ IncludeNode *IncludeTree::Get(const SourceLocation &loc)
237236

238237
/* In case we could not find a FileRef, then try the ExpansionLoc. */
239238
if (!fileref.has_value()) {
240-
SourceManager *SM = PrettyPrint::Get_Source_Manager();
241-
const SourceLocation &loc2 = SM->getExpansionLoc(loc);
239+
const SourceLocation &loc2 = SM.getExpansionLoc(loc);
242240
fileref = PrettyPrint::Get_FileEntry(loc2);
243241
}
244242

@@ -365,12 +363,11 @@ void IncludeTree::IncludeNode::Set_FileEntry(OptionalFileEntryRef file)
365363

366364
SourceRange IncludeTree::IncludeNode::Get_File_Range(void)
367365
{
368-
SourceManager *SM = PrettyPrint::Get_Source_Manager();
369-
370366
SourceLocation start, end;
371-
FileID fid = SM->getOrCreateFileID(*File, SrcMgr::CharacteristicKind());
372-
start = SM->getLocForStartOfFile(fid);
373-
end = SM->getLocForEndOfFile(fid);
367+
SourceManager &SM = Tree.SM;
368+
FileID fid = SM.getOrCreateFileID(*File, SrcMgr::CharacteristicKind());
369+
start = SM.getLocForStartOfFile(fid);
370+
end = SM.getLocForEndOfFile(fid);
374371

375372
assert(start.isValid() && "Start of header is invalid.");
376373
assert(end.isValid() && "End of header is invalid.");
@@ -563,7 +560,7 @@ void IncludeNode::Set_HeaderGuard(MacroDefinitionRecord *guard)
563560
HeaderGuard = guard;
564561
} else {
565562
std::string message = "Attempt to redefine headerguard of " + Get_Filename().str();
566-
DiagsClass::Emit_Warn(message, guard->getSourceRange());
563+
DiagsClass::Emit_Warn(message, guard->getSourceRange(), Tree.SM);
567564
}
568565
}
569566

libcextract/PrettyPrint.hh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ class PrettyPrint
8787
AST = ast;
8888
}
8989

90-
static inline SourceManager *Get_Source_Manager(void)
91-
{
92-
return &AST->getSourceManager();
93-
}
94-
9590
static inline LangOptions &Get_Lang_Options(void)
9691
{
9792
return LangOpts;

libcextract/SymbolExternalizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ void TextModifications::Solve(void)
384384
/* Intersection with the same priority. Issue an error -- we can't have this. */
385385
DiagsClass::Emit_Error("Rewriter ranges with same priority intersects");
386386
DiagsClass::Emit_Note(" This one: (priority " + std::to_string(a.Priority) + ')',
387-
a.ToChange);
387+
a.ToChange, SM);
388388
DiagsClass::Emit_Note("with this: (priority " + std::to_string(b.Priority) + ')',
389-
b.ToChange);
389+
b.ToChange, SM);
390390
throw std::runtime_error("SymbolExternalizer can not continue.");
391391
}
392392
} else {

0 commit comments

Comments
 (0)