Skip to content

[APINotes] Support globals in extern "C++" blocks #7309

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 1 commit into from
Aug 24, 2023
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
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaAPINotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,8 @@ void Sema::ProcessAPINotes(Decl *D) {

// Globals.
if (D->getDeclContext()->isFileContext() ||
D->getDeclContext()->isExternCContext()) {
D->getDeclContext()->isExternCContext() ||
D->getDeclContext()->isExternCXXContext()) {
// Global variables.
if (auto VD = dyn_cast<VarDecl>(D)) {
for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
Expand Down
15 changes: 15 additions & 0 deletions clang/test/APINotes/Inputs/Headers/ExternCtx.apinotes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Name: ExternCtx
Globals:
- Name: globalInExternC
Availability: none
AvailabilityMsg: "oh no"
- Name: globalInExternCXX
Availability: none
AvailabilityMsg: "oh no #2"
Functions:
- Name: globalFuncInExternC
Availability: none
AvailabilityMsg: "oh no #3"
- Name: globalFuncInExternCXX
Availability: none
AvailabilityMsg: "oh no #4"
11 changes: 11 additions & 0 deletions clang/test/APINotes/Inputs/Headers/ExternCtx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern "C" {
static int globalInExternC = 1;

static void globalFuncInExternC() {}
}

extern "C++" {
static int globalInExternCXX = 2;

static void globalFuncInExternCXX() {}
}
4 changes: 4 additions & 0 deletions clang/test/APINotes/Inputs/Headers/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module ExternCtx {
header "ExternCtx.h"
}

module HeaderLib {
header "HeaderLib.h"
}
Expand Down
23 changes: 23 additions & 0 deletions clang/test/APINotes/extern-context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalInExternC -x c++ | FileCheck -check-prefix=CHECK-EXTERN-C %s
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalInExternCXX -x c++ | FileCheck -check-prefix=CHECK-EXTERN-CXX %s
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalFuncInExternC -x c++ | FileCheck -check-prefix=CHECK-FUNC-EXTERN-C %s
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/ModulesCache -fdisable-module-hash -fapinotes-modules -fsyntax-only -I %S/Inputs/Headers %s -ast-dump -ast-dump-filter globalFuncInExternCXX -x c++ | FileCheck -check-prefix=CHECK-FUNC-EXTERN-CXX %s

#include "ExternCtx.h"

// CHECK-EXTERN-C: Dumping globalInExternC:
// CHECK-EXTERN-C: VarDecl {{.+}} imported in ExternCtx globalInExternC 'int'
// CHECK-EXTERN-C: UnavailableAttr {{.+}} <<invalid sloc>> "oh no"

// CHECK-EXTERN-CXX: Dumping globalInExternCXX:
// CHECK-EXTERN-CXX: VarDecl {{.+}} imported in ExternCtx globalInExternCXX 'int'
// CHECK-EXTERN-CXX: UnavailableAttr {{.+}} <<invalid sloc>> "oh no #2"

// CHECK-FUNC-EXTERN-C: Dumping globalFuncInExternC:
// CHECK-FUNC-EXTERN-C: FunctionDecl {{.+}} imported in ExternCtx globalFuncInExternC 'void ()'
// CHECK-FUNC-EXTERN-C: UnavailableAttr {{.+}} <<invalid sloc>> "oh no #3"

// CHECK-FUNC-EXTERN-CXX: Dumping globalFuncInExternCXX:
// CHECK-FUNC-EXTERN-CXX: FunctionDecl {{.+}} imported in ExternCtx globalFuncInExternCXX 'void ()'
// CHECK-FUNC-EXTERN-CXX: UnavailableAttr {{.+}} <<invalid sloc>> "oh no #4"