diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -199,6 +199,7 @@ std::unique_ptr MSSection; std::unique_ptr MSRuntimeChecks; std::unique_ptr MSIntrinsic; + std::unique_ptr MSFunction; std::unique_ptr MSOptimize; std::unique_ptr MSFenvAccess; std::unique_ptr CUDAForceHostDeviceHandler; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -757,6 +757,8 @@ /// optimizations are currently "on", this is set to an invalid location. SourceLocation OptimizeOffPragmaLocation; + std::vector MSFunctionNoBuiltins; + /// QC-Specific. Obfuscate objects in this scope. bool ObfuscateThisScope; /// QC-Specific. Do not obfuscate objects in this scope. @@ -10231,6 +10233,8 @@ /// Called on well formed \#pragma clang optimize. void ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc); + void ActOnPragmaMSFunction(std::vector NoBuiltins); + /// QC-Specific. #pragma obfuscate. void ActOnPragmaObfuscate(SourceLocation Loc, PragmaMsStackAction Action, bool Val); @@ -10253,6 +10257,11 @@ /// attribute to be added (usually because of a pragma). void AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, SourceLocation Loc); + /// Only called on function definitions; if there is a pragma in scope + /// with the effect of a range-based no_builtin, consider marking the function + /// with attribute no_builtin. + void AddRangeBasedNoBuiltin(FunctionDecl *FD); + /// AddAlignedAttr - Adds an aligned attribute to a particular declaration. void AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E, bool IsPackExpansion); diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp --- a/clang/lib/Parse/ParsePragma.cpp +++ b/clang/lib/Parse/ParsePragma.cpp @@ -275,6 +275,15 @@ Token &FirstToken) override; }; +struct PragmaMSFunctionHandler : public PragmaHandler { + PragmaMSFunctionHandler(Sema &S) : PragmaHandler("function"), Actions(S) {} + void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, + Token &FirstToken) override; + +private: + Sema &Actions; +}; + struct PragmaMSOptimizeHandler : public PragmaHandler { PragmaMSOptimizeHandler() : PragmaHandler("optimize") {} void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, @@ -477,6 +486,8 @@ PP.AddPragmaHandler(MSRuntimeChecks.get()); MSIntrinsic = std::make_unique(); PP.AddPragmaHandler(MSIntrinsic.get()); + MSFunction = std::make_unique(Actions); + PP.AddPragmaHandler(MSFunction.get()); MSOptimize = std::make_unique(); PP.AddPragmaHandler(MSOptimize.get()); MSFenvAccess = std::make_unique(); @@ -592,6 +603,8 @@ MSRuntimeChecks.reset(); PP.RemovePragmaHandler(MSIntrinsic.get()); MSIntrinsic.reset(); + PP.RemovePragmaHandler(MSFunction.get()); + MSFunction.reset(); PP.RemovePragmaHandler(MSOptimize.get()); MSOptimize.reset(); PP.RemovePragmaHandler(MSFenvAccess.get()); @@ -3815,6 +3828,50 @@ << "intrinsic"; } +void PragmaMSFunctionHandler::HandlePragma(Preprocessor &PP, + PragmaIntroducer Introducer, + Token &Tok) { + PP.Lex(Tok); + + if (Tok.isNot(tok::l_paren)) { + PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) + << "function"; + return; + } + PP.Lex(Tok); // ( + + bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H"); + + std::vector NoBuiltins; + while (Tok.is(tok::identifier)) { + IdentifierInfo *II = Tok.getIdentifierInfo(); + if (!II->getBuiltinID()) { + PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin) + << II << SuggestIntrinH; + } else { + NoBuiltins.push_back(II->getName()); + } + + PP.Lex(Tok); + if (Tok.isNot(tok::comma)) + break; + PP.Lex(Tok); // , + } + + if (Tok.isNot(tok::r_paren)) { + PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) + << "function"; + return; + } + PP.Lex(Tok); // ) + + if (Tok.isNot(tok::eod)) + PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) + << "function"; + + Actions.ActOnPragmaMSFunction(NoBuiltins); +} + // #pragma optimize("gsty", on|off) void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp --- a/clang/lib/Sema/SemaAttr.cpp +++ b/clang/lib/Sema/SemaAttr.cpp @@ -1044,6 +1044,11 @@ OptimizeOffPragmaLocation = PragmaLoc; } +void Sema::ActOnPragmaMSFunction(std::vector NoBuiltins) { + MSFunctionNoBuiltins.insert(MSFunctionNoBuiltins.end(), + NoBuiltins.begin(), NoBuiltins.end()); +} + // QC-Specific. Begin. void Sema::ActOnPragmaObfuscate(SourceLocation Loc, PragmaMsStackAction Action, bool Val) { @@ -1122,6 +1127,13 @@ FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc)); } +void Sema::AddRangeBasedNoBuiltin(FunctionDecl *FD) { + if (!MSFunctionNoBuiltins.empty()) { + FD->addAttr(NoBuiltinAttr::CreateImplicit(Context, + MSFunctionNoBuiltins.data(), MSFunctionNoBuiltins.size())); + } +} + typedef std::vector > VisStack; enum : unsigned { NoVisibility = ~0U }; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -10190,10 +10190,12 @@ // marking the function. AddCFAuditedAttribute(NewFD); - // If this is a function definition, check if we have to apply optnone due to - // a pragma. - if(D.isFunctionDefinition()) + // If this is a function definition, check if we have to apply any + // attributes (i.e. optnone and no_builtin) due to a pragma. + if(D.isFunctionDefinition()) { AddRangeBasedOptnone(NewFD); + AddRangeBasedNoBuiltin(NewFD); + } // If this is the first declaration of an extern C variable, update // the map of such variables. diff --git a/clang/test/CodeGen/pragma-ms-function.c b/clang/test/CodeGen/pragma-ms-function.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/pragma-ms-function.c @@ -0,0 +1,49 @@ +// RUN: %clang_cc1 -emit-llvm -fms-extensions -o - %s | FileCheck %s + +#include + +void bar(char *s); +void *memset(void *s, int c, size_t n); +void *memcpy(void *d, const void *s, size_t n); + +// CHECK: define{{.*}} void @foo1({{.*}}) #[[NO_NOBUILTIN:[0-9]+]] +// CHECK: call void @bar +// CHECK: call void @llvm.memset +// CHECK: call void @llvm.memcpy +void foo1(char *s, char *d, size_t n) +{ + bar(s); + memset(s, 0, n); + memcpy(d, s, n); +} + +#pragma function(strlen, memset) + +// CHECK: define{{.*}} void @foo2({{.*}}) #[[NOBUILTIN_MEMSET:[0-9]+]] +// CHECK: call void @bar +// CHECK: {{.*}}call i8* @memset +// CHECK: call void @llvm.memcpy +void foo2(char *s, char *d, size_t n) +{ + bar(s); + memset(s, 1, n); + memcpy(d, s, n); +} + +#pragma function(memcpy) + +// CHECK: define{{.*}} void @foo3({{.*}}) #[[NOBUILTIN_MEMSET_MEMCPY:[0-9]+]] +// CHECK: call void @bar +// CHECK: {{.*}}call i8* @memset +// CHECK: {{.*}}call i8* @memcpy +void foo3(char *s, char *d, size_t n) +{ + bar(s); + memset(s, 2, n); + memcpy(d, s, n); +} + +// CHECK-NOT: attributes #[[NO_NOBUILTIN]] = {{{.*}}"no-builtin-memset"{{.*}}} +// CHECK-NOT: attributes #[[NO_NOBUILTIN]] = {{{.*}}"no-builtin-memcpy"{{.*}}"no-builtin-memset"{{.*}}} +// CHECK: attributes #[[NOBUILTIN_MEMSET]] = {{{.*}}"no-builtin-memset"{{.*}}} +// CHECK: attributes #[[NOBUILTIN_MEMSET_MEMCPY]] = {{{.*}}"no-builtin-memcpy"{{.*}}"no-builtin-memset"{{.*}}}