@@ -100,6 +100,7 @@ static cl::opt<bool> TestRun("t", cl::desc("Enable test run"), cl::init(false),
100
100
static cl::extrahelp CommonHelp (CommonOptionsParser::HelpMessage);
101
101
102
102
namespace {
103
+ inline StringRef asRef (StringView S) { return {S.begin (), S.size ()}; }
103
104
class BumpPointerAllocator {
104
105
public:
105
106
BumpPointerAllocator ()
@@ -175,7 +176,7 @@ class DefaultAllocator {
175
176
BumpPointerAllocator Alloc;
176
177
};
177
178
178
- clang::QualType getBaseType (StringView Name, clang::ASTContext *AST,
179
+ clang::QualType getBaseType (StringRef Name, clang::ASTContext *AST,
179
180
bool &IsVariadic) {
180
181
clang::QualType Res;
181
182
// First find the match against `QualType`...
@@ -249,8 +250,7 @@ clang::QualType getBaseType(StringView Name, clang::ASTContext *AST,
249
250
else if (Name == " _BitInt" )
250
251
assert (false && " unhandled type name: _BitInt" );
251
252
else {
252
- StringRef const N{Name.begin (), Name.size ()};
253
- auto &II = AST->Idents .get (N);
253
+ auto &II = AST->Idents .get (Name);
254
254
auto *DC = AST->getTranslationUnitDecl ();
255
255
auto *ED = EnumDecl::Create (*AST, DC, SourceLocation (), SourceLocation (),
256
256
&II, nullptr , false , false , true );
@@ -284,10 +284,10 @@ class Remangler {
284
284
assert (MangleContext->shouldMangleDeclName (FD) &&
285
285
" It should always be possible to mangle libclc func." );
286
286
287
- SmallString< 256 > Buffer ;
288
- raw_svector_ostream Out (Buffer );
287
+ std::string Buf ;
288
+ raw_string_ostream Out (Buf );
289
289
MangleContext->mangleName (FD, Out);
290
- return std::string{Out. str ()} ;
290
+ return Buf ;
291
291
}
292
292
293
293
private:
@@ -339,15 +339,14 @@ class Remangler {
339
339
(Encoding->getName ()->getKind () == Node::Kind::KNameType ||
340
340
Encoding->getName ()->getKind () == Node::Kind::KNameWithTemplateArgs) &&
341
341
" Expected KNameType or KNameWithTemplateArgs node." );
342
- std::string KernelNameStr ;
342
+ StringRef KernelName ;
343
343
if (Encoding->getName ()->getKind () == Node::Kind::KNameType) {
344
344
auto *NT = static_cast <const NameType *>(Encoding->getName ());
345
- KernelNameStr. assign (NT->getBaseName (). begin (), NT-> getBaseName (). size ());
345
+ KernelName = asRef (NT->getBaseName ());
346
346
} else {
347
347
auto *NT = static_cast <const NameWithTemplateArgs *>(Encoding->getName ());
348
- KernelNameStr. assign (NT->getBaseName (). begin (), NT-> getBaseName (). size ());
348
+ KernelName = asRef (NT->getBaseName ());
349
349
}
350
- StringRef const KernelName (KernelNameStr);
351
350
FD->setDeclName (&AST->Idents .get (KernelName));
352
351
353
352
// Construct the argument list.
@@ -481,9 +480,12 @@ class Remangler {
481
480
assert (VecType->getDimension ()->getKind () == Node::Kind::KNameType);
482
481
const auto *Dims = static_cast <const itanium_demangle::NameType *>(
483
482
VecType->getDimension ());
484
- std::string const DN{Dims->getName ().begin (), Dims->getName ().size ()};
485
- auto D = std::atoi (DN.c_str ());
486
- PossibleKinds.push_back (NodeKindInfo (Kind, D));
483
+ size_t DimNum;
484
+ if (asRef (Dims->getName ()).getAsInteger (10 , DimNum) || !DimNum) {
485
+ assert (false && " invalid vector size specifier" );
486
+ break ;
487
+ }
488
+ PossibleKinds.push_back (NodeKindInfo (Kind, DimNum));
487
489
return handleTypeNode (VecType->getBaseType (), PossibleKinds);
488
490
}
489
491
case Node::Kind::KBinaryFPType: {
@@ -493,27 +495,28 @@ class Remangler {
493
495
const auto *NameTypeNode =
494
496
static_cast <const itanium_demangle::NameType *>(
495
497
BFPType->getDimension ());
496
- std::string const D{NameTypeNode->getBaseName ().begin (),
497
- NameTypeNode->getBaseName ().size ()};
498
- assert (D == " 16" && " Unexpected binary floating point type." );
499
- (void )D;
498
+ assert (asRef (NameTypeNode->getBaseName ()) == " 16" &&
499
+ " Unexpected binary floating point type." );
500
500
// BinaryFPType is encoded as: BinaryFPType(NameType("16")), manually
501
501
// construct "_Float16" NamedType node so we can pass it directly to
502
502
// handleLeafTypeNode.
503
503
NameType const FP16{" _Float16" };
504
- return handleLeafTypeNode (& FP16, PossibleKinds);
504
+ return handleLeafTypeNode (FP16. getName () , PossibleKinds);
505
505
}
506
506
case Node::Kind::KVendorExtQualType: {
507
507
const auto *ExtQualType =
508
508
static_cast <const itanium_demangle::VendorExtQualType *>(TypeNode);
509
- std::string const AS (ExtQualType->getExt ().begin (),
510
- ExtQualType->getExt ().size ());
511
- if (AS.rfind (" AS" , 0 ) != 0 ) {
509
+ StringRef AS = asRef (ExtQualType->getExt ());
510
+ if (!AS.starts_with (" AS" )) {
511
+ assert (false && " Unexpected ExtQualType." );
512
+ break ;
513
+ }
514
+ size_t ASNum;
515
+ if (AS.drop_front (2 ).getAsInteger (10 , ASNum)) {
512
516
assert (false && " Unexpected ExtQualType." );
513
517
break ;
514
518
}
515
- auto ASVal = std::atoi (AS.c_str () + 2 );
516
- PossibleKinds.push_back (NodeKindInfo (Kind, ASVal));
519
+ PossibleKinds.push_back ({Kind, ASNum});
517
520
return handleTypeNode (ExtQualType->getTy (), PossibleKinds);
518
521
}
519
522
case Node::Kind::KQualType: {
@@ -523,17 +526,16 @@ class Remangler {
523
526
}
524
527
case Node::Kind::KNameType: {
525
528
auto *NT = static_cast <const itanium_demangle::NameType *>(TypeNode);
526
- return handleLeafTypeNode (NT, PossibleKinds);
529
+ return handleLeafTypeNode (NT-> getName () , PossibleKinds);
527
530
}
528
531
case Node::Kind::KNestedName: {
529
532
const auto *NN =
530
533
static_cast <const itanium_demangle::NestedName *>(TypeNode);
531
534
OutputBuffer QB;
532
535
NN->Qual ->print (QB);
533
536
PossibleKinds.push_back ({Kind, QB.getBuffer (), QB.getCurrentPosition ()});
534
- const auto *TN =
535
- static_cast <const itanium_demangle::NameType *>(NN->Name );
536
- return handleLeafTypeNode (TN, PossibleKinds);
537
+ auto *NT = static_cast <const itanium_demangle::NameType *>(NN->Name );
538
+ return handleLeafTypeNode (NT->getName (), PossibleKinds);
537
539
}
538
540
default : {
539
541
OutputBuffer ErrorTypeOut;
@@ -551,16 +553,20 @@ class Remangler {
551
553
// Handle undecorated type that can be matched against `QualType`, also
552
554
// returning if variadic.
553
555
std::pair<clang::QualType, bool >
554
- handleLeafTypeNode (const NameType *NT ,
556
+ handleLeafTypeNode (StringView Name ,
555
557
SmallVector<NodeKindInfo> &PossibleKinds) {
556
- StringView Name = NT->getName ();
558
+ return handleLeafTypeNode (asRef (Name), PossibleKinds);
559
+ }
560
+
561
+ std::pair<clang::QualType, bool >
562
+ handleLeafTypeNode (StringRef Name, SmallVector<NodeKindInfo> &PossibleKinds) {
557
563
558
564
// When in test run, don't enable replacements and assert that re-mangled
559
565
// name matches the original.
560
566
if (!TestRun) {
561
567
auto It = TypeReplacements.find (Name.begin ());
562
568
if (It != TypeReplacements.end ())
563
- Name = StringView ( It->second ) ;
569
+ Name = It->second ;
564
570
}
565
571
566
572
bool IsVariadic = false ;
@@ -584,7 +590,7 @@ class Remangler {
584
590
std::string const N{KNN->DataStr + " " +
585
591
Res.getBaseTypeIdentifier ()->getName ().str ()};
586
592
if (NestedNamesQTMap.count (N) == 0 ) {
587
- assert (KNN->DataStr . rfind (" __spv:: " , 0 ) == 0 &&
593
+ assert (StringRef ( KNN->DataStr ). starts_with (" __spv" ) &&
588
594
" Unexpected nested prefix" );
589
595
SourceLocation const SL{};
590
596
RecordDecl *RD = nullptr ;
@@ -593,8 +599,8 @@ class Remangler {
593
599
*AST, AST->getTranslationUnitDecl (), false , SL, SL,
594
600
&AST->Idents .get (" __spv" , tok::TokenKind::identifier), nullptr ,
595
601
false );
596
- std::string StructName{KNN-> DataStr };
597
- StructName. erase ( 0 , StructName. find_first_not_of (" __spv::" ));
602
+ std::string StructName =
603
+ StringRef (KNN-> DataStr ). split (" __spv::" ). second . str ( );
598
604
auto *II = &AST->Idents .get (StructName, tok::TokenKind::identifier);
599
605
RD = RecordDecl::Create (*AST, TTK_Struct, SpvNamespace, SL, SL, II);
600
606
auto *NNS = NestedNameSpecifier::Create (*AST, nullptr , SpvNamespace);
@@ -783,7 +789,7 @@ class LibCLCRemangler : public ASTConsumer {
783
789
}
784
790
785
791
private:
786
- bool createClones (llvm::Module *M, std::string OriginalMangledName,
792
+ bool createClones (llvm::Module *M, StringRef OriginalMangledName,
787
793
std::string RemangledName,
788
794
const itanium_demangle::Node *FunctionTree,
789
795
TargetTypeReplacements &Replacements) {
@@ -799,7 +805,7 @@ class LibCLCRemangler : public ASTConsumer {
799
805
}
800
806
801
807
bool
802
- createCloneFromMap (llvm::Module *M, std::string OriginalName,
808
+ createCloneFromMap (llvm::Module *M, StringRef OriginalName,
803
809
const itanium_demangle::Node *FunctionTree,
804
810
SmallDenseMap<const char *, const char *> TypeReplacements,
805
811
bool CloneeTypeReplacement = false ) {
0 commit comments