@@ -8510,22 +8510,26 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8510
8510
}
8511
8511
8512
8512
namespace {
8513
- class PointerParamInfo {
8513
+ class PointerParamInfoPrinter {
8514
8514
public:
8515
- virtual void print (clang::ASTContext &ctx, llvm::raw_ostream &out) const = 0;
8516
- virtual ~PointerParamInfo () = default ;
8517
- };
8515
+ clang::ASTContext &ctx;
8516
+ llvm::raw_ostream &out;
8517
+ bool firstParam = true ;
8518
+ PointerParamInfoPrinter (clang::ASTContext &ctx, llvm::raw_ostream &out)
8519
+ : ctx(ctx), out(out) {
8520
+ out << " @PointerBounds(" ;
8521
+ }
8522
+ ~PointerParamInfoPrinter () { out << " )" ; }
8518
8523
8519
- class CountedByParam : public PointerParamInfo {
8520
- public:
8521
- size_t pointerIndex;
8522
- clang::Expr *countExpr;
8523
- bool isSizedBy;
8524
- CountedByParam (size_t idx, clang::Expr *E, bool sizedBy)
8525
- : pointerIndex(idx), countExpr(E), isSizedBy(sizedBy) {}
8526
-
8527
- virtual void print (clang::ASTContext &ctx,
8528
- llvm::raw_ostream &out) const override {
8524
+ void printCountedBy (const clang::CountAttributedType *CAT,
8525
+ size_t pointerIndex) {
8526
+ if (!firstParam) {
8527
+ out << " , " ;
8528
+ } else {
8529
+ firstParam = false ;
8530
+ }
8531
+ clang::Expr *countExpr = CAT->getCountExpr ();
8532
+ bool isSizedBy = CAT->isCountInBytes ();
8529
8533
out << " ." ;
8530
8534
if (isSizedBy)
8531
8535
out << " sizedBy" ;
@@ -8541,7 +8545,6 @@ class CountedByParam : public PointerParamInfo {
8541
8545
out, {}, {ctx.getLangOpts ()}); // TODO: map clang::Expr to Swift Expr
8542
8546
out << " \" )" ;
8543
8547
}
8544
- virtual ~CountedByParam () {}
8545
8548
};
8546
8549
} // namespace
8547
8550
@@ -8551,43 +8554,31 @@ void ClangImporter::Implementation::importBoundsAttributes(
8551
8554
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl ());
8552
8555
if (!ClangDecl)
8553
8556
return ;
8554
-
8555
- SmallVector<PointerParamInfo *, 4 > BoundsInfo;
8556
- size_t parameterIndex = 1 ;
8557
- for (auto param : ClangDecl->parameters ()) {
8558
- if (auto CAT = param->getType ()->getAs <clang::CountAttributedType>()) {
8559
- BoundsInfo.push_back (new CountedByParam (
8560
- parameterIndex, CAT->getCountExpr (), CAT->isCountInBytes ()));
8561
- }
8562
- parameterIndex++;
8563
- }
8564
- if (BoundsInfo.empty ())
8557
+ if (!funcsWithPointerBounds.count (ClangDecl))
8565
8558
return ;
8566
8559
8567
8560
llvm::SmallString<128 > MacroString;
8568
8561
{
8569
8562
llvm::raw_svector_ostream out (MacroString);
8570
8563
8571
- out << " @PointerBounds( " ;
8572
- for ( size_t i = 0 ; i < BoundsInfo. size (); i++) {
8573
- BoundsInfo[i]-> print ( getClangASTContext (), out);
8574
- if (i + 1 < BoundsInfo. size ()) {
8575
- out << " , " ;
8564
+ size_t parameterIndex = 1 ;
8565
+ PointerParamInfoPrinter printer ( getClangASTContext (), out);
8566
+ for ( auto param : ClangDecl-> parameters ()) {
8567
+ if (auto CAT = param-> getType ()-> getAs <clang::CountAttributedType> ()) {
8568
+ printer. printCountedBy (CAT, parameterIndex) ;
8576
8569
}
8570
+ parameterIndex++;
8577
8571
}
8578
- out << " )" ;
8579
8572
}
8580
8573
8581
- // Dig out a buffer with the attribute text.
8582
- unsigned bufferID = getClangSwiftAttrSourceBuffer (MacroString);
8583
8574
8584
8575
// Dig out a source file we can use for parsing.
8585
8576
auto &sourceFile = getClangSwiftAttrSourceFile (
8586
- *MappedDecl->getDeclContext ()->getParentModule (), bufferID );
8577
+ *MappedDecl->getDeclContext ()->getParentModule (), MacroString );
8587
8578
8588
8579
// Spin up a parser.
8589
- swift::Parser parser (bufferID, sourceFile, &SwiftContext. Diags , nullptr ,
8590
- nullptr );
8580
+ swift::Parser parser (sourceFile. getBufferID (), sourceFile ,
8581
+ &SwiftContext. Diags , nullptr , nullptr );
8591
8582
// Prime the lexer.
8592
8583
parser.consumeTokenWithoutFeedingReceiver ();
8593
8584
0 commit comments