[PATCH] D105457: [clang] Refactor AST printing tests to share more infrastructure

Douglas Yung via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 14 01:39:57 PDT 2021


dyung added a comment.

If it helps, I have so far been able to reduce the file to this which still shows the failure when compiled with gcc 7.5:

  #include "ASTPrint.h"
  #include "clang/AST/ASTContext.h"
  #include "clang/ASTMatchers/ASTMatchFinder.h"
  #include "clang/Tooling/Tooling.h"
  #include "llvm/ADT/SmallString.h"
  #include "gtest/gtest.h"
  
  using namespace clang;
  using namespace ast_matchers;
  //using namespace tooling;                                                                                                                                                                                                                                         
  
  namespace {
  
  enum class StdVer { CXX98, CXX11, CXX14, CXX17, CXX2a };
  
  DeclarationMatcher FunctionBodyMatcher(StringRef ContainingFunction) {
    return functionDecl(hasName(ContainingFunction),
                        has(compoundStmt(has(stmt().bind("id")))));
  }
  
  static void PrintStmt(raw_ostream &Out, const ASTContext *Context,
                        const Stmt *S, PrintingPolicyAdjuster PolicyAdjuster) {
    assert(S != nullptr && "Expected non-null Stmt");
    PrintingPolicy Policy = Context->getPrintingPolicy();
    if (PolicyAdjuster)
      PolicyAdjuster(Policy);
    S->printPretty(Out, /*Helper*/ nullptr, Policy);
  }
  
  template <typename Matcher>
  ::testing::AssertionResult
  PrintedStmtMatches(StringRef Code, const std::vector<std::string> &Args,
                     const Matcher &NodeMatch, StringRef ExpectedPrinted,
                     PrintingPolicyAdjuster PolicyAdjuster = nullptr) {
    return PrintedNodeMatches<Stmt>(Code, Args, NodeMatch, ExpectedPrinted, "",
                                    PrintStmt, PolicyAdjuster);
  }
  
  template <typename T>
  ::testing::AssertionResult
  PrintedStmtCXXMatches(StdVer Standard, StringRef Code, const T &NodeMatch,
                        StringRef ExpectedPrinted,
                        PrintingPolicyAdjuster PolicyAdjuster = nullptr) {
    const char *StdOpt;
    switch (Standard) {
    case StdVer::CXX98: StdOpt = "-std=c++98"; break;
    case StdVer::CXX11: StdOpt = "-std=c++11"; break;
    case StdVer::CXX14: StdOpt = "-std=c++14"; break;
    case StdVer::CXX17: StdOpt = "-std=c++17"; break;
    case StdVer::CXX2a: StdOpt = "-std=c++2a"; break;
    }
  
    std::vector<std::string> Args = {
      StdOpt,
      "-Wno-unused-value",
    };
    return PrintedStmtMatches(Code, Args, NodeMatch, ExpectedPrinted,
                              PolicyAdjuster);
  }
  
  } // unnamed namespace
  
  TEST(StmtPrinter, TestIntegerLiteral) {
    ASSERT_TRUE(PrintedStmtCXXMatches(StdVer::CXX98,
      "void A() {"
      "  1, -1, 1U, 1u,"
      "  1L, 1l, -1L, 1UL, 1ul,"
      "  1LL, -1LL, 1ULL;"
      "}",
      FunctionBodyMatcher("A"),
      "1 , -1 , 1U , 1U , "
      "1L , 1L , -1L , 1UL , 1UL , "
      "1LL , -1LL , 1ULL"));
      // Should be: with semicolon                                                                                                                                                                                                                                   
  }
  
  TEST(StmtPrinter, TestCXXConversionDeclImplicit) {
    ASSERT_TRUE(PrintedStmtCXXMatches(
        StdVer::CXX98,
        "struct A {"
        "operator void *();"
        "A operator&(A);"
        "};"
        "void bar(void *);"
        "void foo(A a, A b) {"
        "  bar(a & b);"
        "}",
        traverse(TK_AsIs, cxxMemberCallExpr(anything()).bind("id")), "a & b"));
  }

When compiled with gcc 7.5, it produces the attached (gzip'd) assembly file which is what is what is giving the errors:

  GNU assembler version 2.30 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.30
  StmtPrinterTest2.s: Assembler messages:
  StmtPrinterTest2.s:928: Error: symbol `_ZNSt14_Function_base13_Base_managerIN5clangUlPKNS1_4StmtEE2_EE10_M_managerERSt9_Any_dataRKS7_St18_Manager_operation' is already defined
  StmtPrinterTest2.s:9924: Error: symbol `_ZNSt17_Function_handlerIFbPKN5clang4StmtEENS0_UlS3_E2_EE9_M_invokeERKSt9_Any_dataOS3_' is already defined{F17926741}


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D105457/new/

https://reviews.llvm.org/D105457



More information about the cfe-commits mailing list