Skip to content

Commit

Permalink
Undefine noinline macro if conflicts with AST dump
Browse files Browse the repository at this point in the history
In kernel sourcecode there is a definition of a macro:
```
```

This seems harmless, but when clang-extract decides to dump the AST of
a Decl, this conflicts with its attr:
```
__attribute__((noinline))
```
which results in the resulting code being broken. Hence, undefine this
macro for the AST dump and define it later.

Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Jul 4, 2024
1 parent dd25987 commit aff6d64
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
34 changes: 33 additions & 1 deletion libcextract/PrettyPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ bool PrettyPrint::Is_Range_Valid(const SourceRange &range)
return Is_Before(begin, end) && (begin != end);
}

bool Has_NoInline_Attr(Decl *decl)
{
const AttrVec &attrs = decl->getAttrs();
for (const Attr *attr : attrs) {
if (attr->getKind() == attr::NoInline) {
return true;
}
}

return false;
}

void PrettyPrint::Print_Decl_Raw(Decl *decl)
{
SourceRange decl_range = decl->getSourceRange();
Expand Down Expand Up @@ -122,7 +134,27 @@ void PrettyPrint::Print_Decl_Raw(Decl *decl)
decl->print(Out, PPolicy);
}
} else {
MacroInfo *noinline_info = nullptr;
if (Has_NoInline_Attr(decl)) {
/* If the code has a `noinline` macro defined, it conflicts with
clang's __attribute__((noinline)) attribute dump. Hence we
have to undef it. */
Preprocessor &pp = AST->getPreprocessor();
MacroWalker MW(pp);

/* Get the valid macro at the source location. */
IdentifierInfo *noinline = pp.getIdentifierInfo("noinline");
noinline_info = MW.Get_Macro_Info(noinline, decl->getBeginLoc());
/* Only print it if the noinline macro is defined at location. */
if (noinline_info) {
Out << "#undef noinline\n";
}
}
decl->print(Out, LangOpts);
if (noinline_info) {
/* Redeclare the macro to the previous value. */
PrettyPrint::Print_MacroInfo(noinline_info);
}
}
} else {
Out << decl_source;
Expand Down Expand Up @@ -192,7 +224,7 @@ void PrettyPrint::Debug_InclusionDirective(InclusionDirective *include)
void PrettyPrint::Print_MacroInfo(MacroInfo *info)
{
SourceRange range(info->getDefinitionLoc(), info->getDefinitionEndLoc());
llvm::outs() << " info: " << Get_Source_Text(range) << '\n';
Out << "#define " << Get_Source_Text(range) << '\n';
}

void PrettyPrint::Print_Attr(Attr *attr)
Expand Down
13 changes: 13 additions & 0 deletions testsuite/small/attr-8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=func_f -DCE_NO_EXTERNALIZATION" }*/
#define MACRO 0

/* Do this sort of blurry definition to confuse out clang-extract into dumping
the AST of the function. */
#define DEFINE_FUNCTION(name) __attribute__((noinline)) int func_##name(void) { return MACRO; }
#define DEFINE_VARIABLE(name) int var_##name;
#define DEFINE(f) DEFINE_VARIABLE(f) DEFINE_FUNCTION(f)

DEFINE(f)

/* { dg-final { scan-tree-dump "__attribute__\(\(noinline\)\)" } } */
/* { dg-final { scan-tree-dump-not "#define noinline" } } */
33 changes: 33 additions & 0 deletions testsuite/small/attr-9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=func_f,g,h -DCE_NO_EXTERNALIZATION" }*/


#define noinline SHOULD_NOT_BE_IN_THE_FINAL_OUTPUT

#undef noinline
#define noinline __attribute__((__noinline__))

/* Do this sort of blurry definition to confuse out clang-extract into dumping
the AST of the function. */
#define DEFINE_FUNCTION(name) noinline int func_##name(void) { return 0; }
#define DEFINE_VARIABLE(name) int var_##name;
#define DEFINE(f) DEFINE_VARIABLE(f) DEFINE_FUNCTION(f)

noinline int g()
{
return 1;
}

DEFINE(f);

noinline int h()
{
return 2;
}


#undef noinline
#define noinline SHOULD_NOT_BE_IN_THE_FINAL_OUTPUT

/* { dg-final { scan-tree-dump "__attribute__\(\(noinline\)\) int func_f|int func_f\(void\) __attribute__\(\(noinline\)\)" } } */
/* { dg-final { scan-tree-dump "#undef noinline" } } */
/* { dg-final { scan-tree-dump-not "SHOULD_NOT_BE_IN_THE_FINAL_OUTPUT" } } */

0 comments on commit aff6d64

Please sign in to comment.