Skip to content

Dumping variables declared as __attribute__((unused)) are dumped incorrectly. #59973

Closed
@giulianobelinassi

Description

@giulianobelinassi

Given the following code:

#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/Tooling.h"

using namespace clang;
using namespace llvm;

std::string Output_AST(ASTUnit *ast)
{
  std::string ret;
  raw_string_ostream ostream(ret);

  for (auto it = ast->top_level_begin(); it != ast->top_level_end(); ++it) {
    Decl *decl = *it;
    decl->print(ostream, PrintingPolicy(LangOptions()));
  }

  return ret;
}

static const char *const code =
"int main() {\n"
"    int x __attribute__((unused)) = 0;\n"
"    return 0;\n"
"}\n";

int main(int argc, char **argv)
{
  std::unique_ptr<ASTUnit> ast = tooling::buildASTFromCode(code);

  std::string output = Output_AST(ast.get());

  if (output != code) {
    llvm::outs() << "Code output by Output_AST:" << '\n'
                 << output;
    return 1;
  }

  return 0;
}

The correct output would be exactly what code variable described, instead of having as output in Output_AST

 int x = 0 __attribute__((unused));

which Clang parser rejects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions