Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -4296,10 +4296,11 @@ /// Valid types should not have multiple attributes with different CCs. const AttributedType *getCallingConvAttributedType(QualType T) const; - /// Stmt attributes - this routine is the top level dispatcher. - StmtResult ProcessStmtAttributes(Stmt *Stmt, - const ParsedAttributesView &Attrs, - SourceRange Range); + /// Process the attributes before creating an attributed statement. Returns + /// the semantic attributes that have been processed. + void ProcessStmtAttributes(Stmt *Stmt, + const ParsedAttributesWithRange &InAttrs, + SmallVectorImpl &OutAttrs); void WarnConflictingTypedMethods(ObjCMethodDecl *Method, ObjCMethodDecl *MethodDecl, @@ -4638,8 +4639,9 @@ StmtResult ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, SourceLocation ColonLoc, Stmt *SubStmt); - StmtResult ActOnAttributedStmt(SourceLocation AttrLoc, - ArrayRef Attrs, + StmtResult BuildAttributedStmt(SourceLocation AttrsLoc, + ArrayRef Attrs, Stmt *SubStmt); + StmtResult ActOnAttributedStmt(const ParsedAttributesWithRange &AttrList, Stmt *SubStmt); class ConditionResult; Index: clang/lib/Parse/ParseStmt.cpp =================================================================== --- clang/lib/Parse/ParseStmt.cpp +++ clang/lib/Parse/ParseStmt.cpp @@ -120,7 +120,7 @@ if (Attrs.empty() || Res.isInvalid()) return Res; - return Actions.ProcessStmtAttributes(Res.get(), Attrs, Attrs.Range); + return Actions.ActOnAttributedStmt(Attrs, Res.get()); } namespace { @@ -657,8 +657,7 @@ SubStmt = ParseStatementOrDeclarationAfterAttributes(Stmts, StmtCtx, nullptr, TempAttrs); if (!TempAttrs.empty() && !SubStmt.isInvalid()) - SubStmt = Actions.ProcessStmtAttributes(SubStmt.get(), TempAttrs, - TempAttrs.Range); + SubStmt = Actions.ActOnAttributedStmt(TempAttrs, SubStmt.get()); } else { Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi; } @@ -1144,7 +1143,7 @@ ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); R = handleExprStmt(Res, SubStmtCtx); if (R.isUsable()) - R = Actions.ProcessStmtAttributes(R.get(), attrs, attrs.Range); + R = Actions.ActOnAttributedStmt(attrs, R.get()); } } Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ clang/lib/Sema/SemaStmt.cpp @@ -555,12 +555,22 @@ return LS; } -StmtResult Sema::ActOnAttributedStmt(SourceLocation AttrLoc, - ArrayRef Attrs, +StmtResult Sema::BuildAttributedStmt(SourceLocation AttrsLoc, + ArrayRef Attrs, Stmt *SubStmt) { - // Fill in the declaration and return it. - AttributedStmt *LS = AttributedStmt::Create(Context, AttrLoc, Attrs, SubStmt); - return LS; + return AttributedStmt::Create(Context, AttrsLoc, Attrs, SubStmt); +} + +StmtResult Sema::ActOnAttributedStmt(const ParsedAttributesWithRange &Attrs, + Stmt *SubStmt) { + SmallVector SemanticAttrs; + ProcessStmtAttributes(SubStmt, Attrs, SemanticAttrs); + if (!SemanticAttrs.empty()) + return BuildAttributedStmt(Attrs.Range.getBegin(), SemanticAttrs, SubStmt); + // If none of the attributes applied, that's fine, we can recover by + // returning the substatement directly instead of making an AttributedStmt + // with no attributes on it. + return SubStmt; } namespace { Index: clang/lib/Sema/SemaStmtAttr.cpp =================================================================== --- clang/lib/Sema/SemaStmtAttr.cpp +++ clang/lib/Sema/SemaStmtAttr.cpp @@ -413,19 +413,13 @@ } } -StmtResult Sema::ProcessStmtAttributes(Stmt *S, - const ParsedAttributesView &AttrList, - SourceRange Range) { - SmallVector Attrs; - for (const ParsedAttr &AL : AttrList) { - if (Attr *a = ProcessStmtAttribute(*this, S, AL, Range)) - Attrs.push_back(a); +void Sema::ProcessStmtAttributes(Stmt *S, + const ParsedAttributesWithRange &InAttrs, + SmallVectorImpl &OutAttrs) { + for (const ParsedAttr &AL : InAttrs) { + if (const Attr *A = ProcessStmtAttribute(*this, S, AL, InAttrs.Range)) + OutAttrs.push_back(A); } - CheckForIncompatibleAttributes(*this, Attrs); - - if (Attrs.empty()) - return S; - - return ActOnAttributedStmt(Range.getBegin(), Attrs, S); + CheckForIncompatibleAttributes(*this, OutAttrs); } Index: clang/lib/Sema/TreeTransform.h =================================================================== --- clang/lib/Sema/TreeTransform.h +++ clang/lib/Sema/TreeTransform.h @@ -1311,9 +1311,9 @@ /// By default, performs semantic analysis to build the new statement. /// Subclasses may override this routine to provide different behavior. StmtResult RebuildAttributedStmt(SourceLocation AttrLoc, - ArrayRef Attrs, + ArrayRef Attrs, Stmt *SubStmt) { - return SemaRef.ActOnAttributedStmt(AttrLoc, Attrs, SubStmt); + return SemaRef.BuildAttributedStmt(AttrLoc, Attrs, SubStmt); } /// Build a new "if" statement.