Skip to content

Commit 099c152

Browse files
authored
[AST] Print the separator "," for template arguments in ConceptReference::print (#91750)
1 parent ef71c79 commit 099c152

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

clang/lib/AST/ASTConcept.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/AST/ASTContext.h"
1616
#include "clang/AST/PrettyPrinter.h"
1717
#include "llvm/ADT/ArrayRef.h"
18+
#include "llvm/ADT/StringExtras.h"
1819

1920
using namespace clang;
2021

@@ -106,9 +107,12 @@ void ConceptReference::print(llvm::raw_ostream &OS,
106107
ConceptName.printName(OS, Policy);
107108
if (hasExplicitTemplateArgs()) {
108109
OS << "<";
110+
llvm::ListSeparator Sep(", ");
109111
// FIXME: Find corresponding parameter for argument
110-
for (auto &ArgLoc : ArgsAsWritten->arguments())
112+
for (auto &ArgLoc : ArgsAsWritten->arguments()) {
113+
OS << Sep;
111114
ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
115+
}
112116
OS << ">";
113117
}
114118
}

clang/unittests/AST/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_clang_unittest(ASTTests
2424
CommentLexer.cpp
2525
CommentParser.cpp
2626
CommentTextTest.cpp
27+
ConceptPrinterTest.cpp
2728
DataCollectionTest.cpp
2829
DeclPrinterTest.cpp
2930
DeclTest.cpp
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===- unittests/AST/ConceptPrinterTest.cpp --- Concept printer tests -----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "ASTPrint.h"
10+
#include "clang/AST/ASTConcept.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/ExprConcepts.h"
13+
#include "clang/ASTMatchers/ASTMatchFinder.h"
14+
#include "clang/Tooling/Tooling.h"
15+
#include "llvm/ADT/SmallString.h"
16+
#include "gtest/gtest.h"
17+
18+
using namespace clang;
19+
using namespace ast_matchers;
20+
using namespace tooling;
21+
22+
namespace {
23+
24+
static void PrintConceptReference(raw_ostream &Out, const ASTContext *Context,
25+
const ConceptSpecializationExpr *T,
26+
PrintingPolicyAdjuster PolicyAdjuster) {
27+
assert(T && T->getConceptReference() &&
28+
"Expected non-null concept reference");
29+
30+
PrintingPolicy Policy = Context->getPrintingPolicy();
31+
if (PolicyAdjuster)
32+
PolicyAdjuster(Policy);
33+
T->getConceptReference()->print(Out, Policy);
34+
}
35+
36+
::testing::AssertionResult
37+
PrintedConceptMatches(StringRef Code, const std::vector<std::string> &Args,
38+
const StatementMatcher &NodeMatch,
39+
StringRef ExpectedPrinted) {
40+
return PrintedNodeMatches<ConceptSpecializationExpr>(
41+
Code, Args, NodeMatch, ExpectedPrinted, "", PrintConceptReference);
42+
}
43+
const internal::VariadicDynCastAllOfMatcher<Stmt, ConceptSpecializationExpr>
44+
conceptSpecializationExpr;
45+
} // unnamed namespace
46+
47+
TEST(ConceptPrinter, ConceptReference) {
48+
std::string Code = R"cpp(
49+
template <typename, typename> concept D = true;
50+
template<typename T, typename U>
51+
requires D<T, U>
52+
void g(T);
53+
)cpp";
54+
auto Matcher = conceptSpecializationExpr().bind("id");
55+
56+
ASSERT_TRUE(PrintedConceptMatches(Code, {"-std=c++20"}, Matcher, "D<T, U>"));
57+
}

llvm/utils/gn/secondary/clang/unittests/AST/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ unittest("ASTTests") {
3232
"CommentLexer.cpp",
3333
"CommentParser.cpp",
3434
"CommentTextTest.cpp",
35+
"ConceptPrinterTest.cpp",
3536
"DataCollectionTest.cpp",
3637
"DeclPrinterTest.cpp",
3738
"DeclTest.cpp",

0 commit comments

Comments
 (0)