Skip to content

Commit 6b9a4ea

Browse files
Fix Remove_Redundant_Decls removing necessary decls
PrettyPrint::Contains_From_LineCol had a bug in which Decls that do not contain themselves were marked as such because two files ended up having the same FileID because of preprocessed notation (example: # 1 "file"). Fix this by dropping this function in favor of Range_Fully_Contains_Range, which uses the expanded location. Signed-off-by: Giuliano Belinassi <[email protected]>
1 parent 4a9b5e3 commit 6b9a4ea

File tree

10 files changed

+46605
-49
lines changed

10 files changed

+46605
-49
lines changed

libcextract/FunctionDepsFinder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void FunctionDependencyFinder::Remove_Redundant_Decls(void)
122122
PrettyPrint::Get_Source_Text(type_range) != "") {
123123

124124
/* Using .fullyContains() fails in some declarations. */
125-
if (PrettyPrint::Contains_From_LineCol(range, type_range)) {
125+
if (Range_Fully_Contains_Range(AST, range, type_range)) {
126126
closure.Remove_Decl(typedecl);
127127
}
128128
}
@@ -195,7 +195,7 @@ void FunctionDependencyFinder::Remove_Redundant_Decls(void)
195195
SourceRange type_range = typedecl->getSourceRange();
196196

197197
/* Using .fullyContains() fails in some declarations. */
198-
if (PrettyPrint::Contains_From_LineCol(range, type_range)) {
198+
if (Range_Fully_Contains_Range(AST, range, type_range)) {
199199
closure.Remove_Decl(typedecl);
200200
}
201201
}
@@ -266,8 +266,8 @@ void FunctionDependencyFinder::Remove_Redundant_Decls(void)
266266
*/
267267
if (PrettyPrint::Get_Source_Text(decl->getSourceRange()) != "" &&
268268
PrettyPrint::Get_Source_Text(prev->getSourceRange()) != "" &&
269-
PrettyPrint::Contains_From_LineCol(decl->getSourceRange(),
270-
prev->getSourceRange())) {
269+
Range_Fully_Contains_Range(AST, decl->getSourceRange(),
270+
prev->getSourceRange())) {
271271
/*
272272
* If the prev and the current decl have the same start LoC, but
273273
* different ending, remove the prev from the closure and set the

libcextract/LLVMMisc.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,14 @@ bool Is_Decl_Equivalent_To(Decl *a, Decl *b)
315315

316316
return a_str == b_str;
317317
}
318+
319+
bool Range_Fully_Contains_Range(ASTUnit *ast, const SourceRange &a,
320+
const SourceRange &b)
321+
{
322+
SourceManager &SM = ast->getSourceManager();
323+
324+
SourceRange a_exp(SM.getExpansionLoc(a.getBegin()), SM.getExpansionLoc(a.getEnd()));
325+
SourceRange b_exp(SM.getExpansionLoc(b.getBegin()), SM.getExpansionLoc(b.getEnd()));
326+
327+
return a_exp.fullyContains(b_exp);
328+
}

libcextract/LLVMMisc.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,6 @@ DeclContextLookupResult Get_Decl_From_Symtab(ASTUnit *ast, const StringRef &name
109109

110110
/** Check if two Decls are equivalent. */
111111
bool Is_Decl_Equivalent_To(Decl *a, Decl *b);
112+
113+
bool Range_Fully_Contains_Range(ASTUnit *ast, const SourceRange &a,
114+
const SourceRange &b);

libcextract/PrettyPrint.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -309,47 +309,6 @@ void PrettyPrint::Debug_SourceLoc(const SourceLocation &loc)
309309
loc.dump(AST->getSourceManager());
310310
}
311311

312-
bool PrettyPrint::Contains_From_LineCol(const SourceRange &a, const SourceRange &b)
313-
{
314-
SourceManager &SM = AST->getSourceManager();
315-
PresumedLoc a_begin = SM.getPresumedLoc(a.getBegin());
316-
PresumedLoc a_end = SM.getPresumedLoc(a.getEnd());
317-
PresumedLoc b_begin = SM.getPresumedLoc(b.getBegin());
318-
PresumedLoc b_end = SM.getPresumedLoc(b.getEnd());
319-
320-
assert(a_begin.getFileID() == a_end.getFileID());
321-
assert(b_begin.getFileID() == b_end.getFileID());
322-
323-
if (a_begin.getFileID() != b_begin.getFileID()) {
324-
/* Files are distinct, thus we can't easily determine which comes first. */
325-
return false;
326-
}
327-
328-
bool a_begin_smaller = false;
329-
bool b_end_smaller = false;
330-
331-
if ((a_begin.getLine() < b_begin.getLine()) ||
332-
(a_begin.getLine() == b_begin.getLine() && a_begin.getColumn() <= b_begin.getColumn())) {
333-
a_begin_smaller = true;
334-
}
335-
336-
if ((b_end.getLine() < a_end.getLine()) ||
337-
(b_end.getLine() == a_end.getLine() && b_end.getColumn() <= a_end.getColumn())) {
338-
b_end_smaller = true;
339-
}
340-
341-
return a_begin_smaller && b_end_smaller;
342-
}
343-
344-
bool PrettyPrint::Contains(const SourceRange &a, const SourceRange &b)
345-
{
346-
if (a.fullyContains(b)) {
347-
return true;
348-
}
349-
350-
return Contains_From_LineCol(a, b);
351-
}
352-
353312
/** Compare if SourceLocation a is after SourceLocation b in the source code. */
354313
bool PrettyPrint::Is_After(const SourceLocation &a, const SourceLocation &b)
355314
{

libcextract/PrettyPrint.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ class PrettyPrint
7878

7979
static void Debug_SourceLoc(const SourceLocation &loc);
8080

81-
static bool Contains_From_LineCol(const SourceRange &a, const SourceRange &b);
82-
83-
static bool Contains(const SourceRange &a, const SourceRange &b);
84-
8581
static inline void Set_AST(ASTUnit *ast)
8682
{
8783
AST = ast;

0 commit comments

Comments
 (0)