Skip to content

[clang][Sema] Declare builtins used in #pragma intrinsic #138205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions clang/lib/Parse/ParsePragma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,13 @@ struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
};

struct PragmaMSIntrinsicHandler : public PragmaHandler {
PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
PragmaMSIntrinsicHandler(Sema &Actions)
: PragmaHandler("intrinsic"), Actions(Actions) {}
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &FirstToken) override;

private:
Sema &Actions;
};

// "\#pragma fenv_access (on)".
Expand Down Expand Up @@ -517,7 +521,7 @@ void Parser::initializePragmaHandlers() {
PP.AddPragmaHandler(MSOptimize.get());
MSRuntimeChecks = std::make_unique<PragmaMSRuntimeChecksHandler>();
PP.AddPragmaHandler(MSRuntimeChecks.get());
MSIntrinsic = std::make_unique<PragmaMSIntrinsicHandler>();
MSIntrinsic = std::make_unique<PragmaMSIntrinsicHandler>(Actions);
PP.AddPragmaHandler(MSIntrinsic.get());
MSFenvAccess = std::make_unique<PragmaMSFenvAccessHandler>();
PP.AddPragmaHandler(MSFenvAccess.get());
Expand Down Expand Up @@ -3803,7 +3807,15 @@ void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
if (!II->getBuiltinID())
PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
<< II << SuggestIntrinH;

// If the builtin hasn't already been declared, declare it now.
DeclarationNameInfo NameInfo(II, Tok.getLocation());
LookupResult Previous(Actions, NameInfo, Sema::LookupOrdinaryName,
Actions.forRedeclarationInCurContext());
Actions.LookupName(Previous, Actions.getCurScope(),
/*CreateBuiltins*/ false);
if (Previous.empty())
Actions.LazilyCreateBuiltin(II, II->getBuiltinID(), Actions.getCurScope(),
/*ForRedeclaration*/ true, Tok.getLocation());
PP.Lex(Tok);
if (Tok.isNot(tok::comma))
break;
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Sema/Inputs/builtin-system-header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifdef USE_PRAGMA_BEFORE
#pragma intrinsic(_InterlockedOr64)
#endif

#define MACRO(x,y) _InterlockedOr64(x,y);

#ifdef USE_PRAGMA_AFTER
#pragma intrinsic(_InterlockedOr64)
#endif
25 changes: 25 additions & 0 deletions clang/test/Sema/builtin-pragma-intrinsic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_BEFORE
// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_AFTER
// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_AFTER_USE
// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s -DUSE_PRAGMA_SAME_FILE
// RUN: %clang_cc1 -fms-extensions -fsyntax-only -verify -triple arm64-windows -isystem %S/Inputs %s

#if defined(USE_PRAGMA_BEFORE) || defined(USE_PRAGMA_AFTER) || defined(USE_PRAGMA_SAME_FILE)
// expected-no-diagnostics
#else
// expected-error@+10 {{call to undeclared library function '_InterlockedOr64'}}
// expected-note@+9 {{include the header <intrin.h> or explicitly provide a declaration for '_InterlockedOr64'}}
#endif
#include <builtin-system-header.h>

#ifdef USE_PRAGMA_SAME_FILE
#pragma intrinsic(_InterlockedOr64)
#endif

void foo() {
MACRO(0,0);
}

#ifdef USE_PRAGMA_AFTER_USE
#pragma intrinsic(_InterlockedOr64)
#endif