Skip to content

[ClangImporter] Query Preprocessor::macros instead of getPreprocessingRecord for header printing #65868

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
May 12, 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
43 changes: 18 additions & 25 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,33 +3029,26 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
}

// Macros.
if (auto *ppRec = ClangPP.getPreprocessingRecord()) {
clang::SourceLocation B = ClangSM.getLocForStartOfFile(FID);
clang::SourceLocation E = ClangSM.getLocForEndOfFile(FID);
clang::SourceRange R(B, E);
const auto &Entities = ppRec->getPreprocessedEntitiesInRange(R);
for (auto I = Entities.begin(), E = Entities.end(); I != E; ++I) {
if (!ppRec->isEntityInFileID(I, FID))
continue;
clang::PreprocessedEntity *PPE = *I;
if (!PPE)
continue;
if (auto *MDR = dyn_cast<clang::MacroDefinitionRecord>(PPE)) {
auto *II = const_cast<clang::IdentifierInfo*>(MDR->getName());
auto MD = ClangPP.getMacroDefinition(II);
if (auto macroNode = getClangNodeForMacroDefinition(MD)) {
if (filter(macroNode)) {
auto MI = macroNode.getAsMacro();
Identifier Name = Impl.getNameImporter().importMacroName(II, MI);
if (Decl *imported = Impl.importMacro(Name, macroNode))
receiver(imported);
}
}
}
}
for (const auto &Iter : ClangPP.macros()) {
auto *II = Iter.first;
auto MD = ClangPP.getMacroDefinition(II);
MD.forAllDefinitions([&](clang::MacroInfo *Info) {
if (Info->isBuiltinMacro())
return;

auto Loc = Info->getDefinitionLoc();
if (Loc.isInvalid() || ClangSM.getFileID(Loc) != FID)
return;

// FIXME: Module imports inside that header.
ClangNode MacroNode = Info;
if (filter(MacroNode)) {
auto Name = Impl.getNameImporter().importMacroName(II, Info);
if (auto *Imported = Impl.importMacro(Name, MacroNode))
receiver(Imported);
}
});
}
// FIXME: Module imports inside that header.
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions test/IDE/Inputs/print_clang_header/header-to-print.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#undef MACRO_GOT_UNDEFINED
#define MY_MACRO 1

#define MACRO_DUP 2
#define MACRO_DUP 3

void doSomethingInHead(int arg);

@interface BaseInHead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Dispatch

var MY_MACRO: Int32 { get }

var MACRO_DUP: Int32 { get }

func doSomethingInHead(_ arg: Int32)

class BaseInHead {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

var MY_MACRO: Int32 { get }

var MACRO_DUP: Int32 { get }

func doSomethingInHead(_ arg: Int32)

class BaseInHead {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Dispatch

var MY_MACRO: Int32 { get }

var MACRO_DUP: Int32 { get }

func doSomethingInHead(_ arg: Int32)

class BaseInHead {
Expand Down
9 changes: 2 additions & 7 deletions test/IDE/print_clang_header.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// FIXME: rdar://problem/19648117 Needs splitting objc parts out
// REQUIRES: objc_interop

// REQUIRES: rdar102151774

// RUN: echo '#include "header-to-print.h"' > %t.m
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -source-filename %s -print-header -header-to-print %S/Inputs/print_clang_header/header-to-print.h -print-regular-comments -enable-objc-interop -disable-objc-attr-requires-foundation-module --cc-args %target-cc-options -isysroot %clang-importer-sdk-path -fsyntax-only %t.m -I %S/Inputs/print_clang_header > %t.txt
// RUN: diff -u %S/Inputs/print_clang_header/header-to-print.h.printed.txt %t.txt
Expand All @@ -19,9 +17,6 @@
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -source-filename %s -print-header -header-to-print %S/Inputs/print_clang_header/header-to-print.h -print-regular-comments -enable-objc-interop -disable-objc-attr-requires-foundation-module --cc-args %target-cc-options -isysroot %clang-importer-sdk-path -fsyntax-only %t.framework.m -F %t -ivfsoverlay %t.yaml -Xclang -fmodule-name=Foo > %t.framework.txt
// RUN: diff -u %S/Inputs/print_clang_header/header-to-print.h.printed.txt %t.framework.txt

// Test header interface printing from a clang module, with the preprocessing record enabled before the CC args.
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -source-filename %s -print-header -header-to-print %S/Inputs/print_clang_header/header-to-print.h -print-regular-comments -Xcc -Xclang -Xcc -detailed-preprocessing-record -enable-objc-interop -disable-objc-attr-requires-foundation-module --cc-args %target-cc-options -isysroot %clang-importer-sdk-path -fsyntax-only %t.framework.m -F %t -ivfsoverlay %t.yaml > %t.module.txt
// Test header interface printing from a clang module.
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -source-filename %s -print-header -header-to-print %S/Inputs/print_clang_header/header-to-print.h -print-regular-comments -enable-objc-interop -disable-objc-attr-requires-foundation-module --cc-args %target-cc-options -isysroot %clang-importer-sdk-path -fsyntax-only %t.framework.m -F %t -ivfsoverlay %t.yaml > %t.module.txt
// RUN: diff -u %S/Inputs/print_clang_header/header-to-print.h.module.printed.txt %t.module.txt
// Test header interface printing from a clang module, with the preprocessing record enabled by the CC args.
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -source-filename %s -print-header -header-to-print %S/Inputs/print_clang_header/header-to-print.h -print-regular-comments -enable-objc-interop -disable-objc-attr-requires-foundation-module --cc-args %target-cc-options -isysroot %clang-importer-sdk-path -fsyntax-only %t.framework.m -F %t -ivfsoverlay %t.yaml -Xclang -detailed-preprocessing-record > %t.module2.txt
// RUN: diff -u %S/Inputs/print_clang_header/header-to-print.h.module.printed.txt %t.module2.txt