Skip to content

[ClangImporter] Record C declarations found in @interfaces. #1449

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
Mar 14, 2016
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
63 changes: 51 additions & 12 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ namespace {
}
};

class HeaderParsingASTConsumer : public clang::ASTConsumer {
SmallVector<clang::DeclGroupRef, 4> DeclGroups;
public:
void
HandleTopLevelDeclInObjCContainer(clang::DeclGroupRef decls) override {
DeclGroups.push_back(decls);
}

ArrayRef<clang::DeclGroupRef> getAdditionalParsedDecls() {
return DeclGroups;
}

void reset() {
DeclGroups.clear();
}
};

class ParsingAction : public clang::ASTFrontendAction {
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &CI, StringRef InFile) override {
return llvm::make_unique<HeaderParsingASTConsumer>();
}
};

class StdStringMemBuffer : public llvm::MemoryBuffer {
const std::string storage;
const std::string name;
Expand Down Expand Up @@ -579,7 +603,7 @@ ClangImporter::create(ASTContext &ctx,
instance.setInvocation(&*invocation);

// Create the associated action.
importer->Impl.Action.reset(new clang::SyntaxOnlyAction);
importer->Impl.Action.reset(new ParsingAction);
auto *action = importer->Impl.Action.get();

// Execute the action. We effectively inline most of
Expand Down Expand Up @@ -640,6 +664,9 @@ ClangImporter::create(ASTContext &ctx,
for (auto path : searchPathOpts.ImportSearchPaths)
importer->addSearchPath(path, /*isFramework*/false);

// FIXME: These decls are not being parsed correctly since (a) some of the
// callbacks are still being added, and (b) the logic to parse them has
// changed.
clang::Parser::DeclGroupPtrTy parsed;
while (!importer->Impl.Parser->ParseTopLevelDecl(parsed)) {
for (auto *D : parsed.get()) {
Expand Down Expand Up @@ -812,28 +839,40 @@ bool ClangImporter::Implementation::importHeader(
/*LoadedID=*/0,
/*LoadedOffset=*/0,
includeLoc);
auto &consumer =
static_cast<HeaderParsingASTConsumer &>(Instance->getASTConsumer());
consumer.reset();

pp.EnterSourceFile(bufferID, /*directoryLookup=*/nullptr, /*loc=*/{});
// Force the import to occur.
pp.LookAhead(0);

SmallVector<clang::NamedDecl *, 16> parsedNamedDecls;
clang::Parser::DeclGroupPtrTy parsed;
while (!Parser->ParseTopLevelDecl(parsed)) {
if (!parsed) continue;

for (auto *D : parsed.get()) {
if (trackParsedSymbols)
SmallVector<clang::DeclGroupRef, 16> allParsedDecls;
auto handleParsed = [&](clang::DeclGroupRef parsed) {
if (trackParsedSymbols) {
for (auto *D : parsed) {
addBridgeHeaderTopLevelDecls(D);
if (auto named = dyn_cast<clang::NamedDecl>(D))
parsedNamedDecls.push_back(named);
}
}

allParsedDecls.push_back(parsed);
};

clang::Parser::DeclGroupPtrTy parsed;
while (!Parser->ParseTopLevelDecl(parsed)) {
if (parsed)
handleParsed(parsed.get());
for (auto additionalParsedGroup : consumer.getAdditionalParsedDecls())
handleParsed(additionalParsedGroup);
consumer.reset();
}

// We can't do this as we're parsing because we may want to resolve naming
// conflicts between the things we've parsed.
for (auto *named : parsedNamedDecls)
addEntryToLookupTable(getClangSema(), BridgingHeaderLookupTable, named);
for (auto group : allParsedDecls)
for (auto *D : group)
if (auto named = dyn_cast<clang::NamedDecl>(D))
addEntryToLookupTable(getClangSema(), BridgingHeaderLookupTable, named);

pp.EndSourceFile();
bumpGeneration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ void doSomethingPartialSub(PartialSubClass *arg);
- (NSObject *)unsafeOverridePartialSubParam:(NSObject *)arg;
- (PartialSubClass *)unsafeOverridePartialSubReturn:(PartialSubClass *)arg;
@end

@interface WrapperInterface
typedef int NameInInterface;
@end

@protocol WrapperProto
typedef int NameInProtocol;
@end

@interface WrapperInterface (Category)
typedef int NameInCategory;
@end
12 changes: 12 additions & 0 deletions test/ClangModules/MixedSource/Inputs/mixed-target/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ typedef NS_ENUM(short, AALevel) {
@end
@interface ConflictingName2
@end

@interface WrapperInterface
typedef int NameInInterface;
@end

@protocol WrapperProto
typedef int NameInProtocol;
@end

@interface WrapperInterface (Category)
typedef int NameInCategory;
@end
6 changes: 6 additions & 0 deletions test/ClangModules/MixedSource/mixed-target-using-header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ func testProtocolNamingConflict() {
d = c // expected-error {{cannot assign value of type 'ConflictingName2?' to type 'ConflictingName2Protocol?'}}
_ = d
}

func testDeclsNestedInObjCContainers() {
let _: NameInInterface = 0
let _: NameInProtocol = 0
let _: NameInCategory = 0
}
5 changes: 5 additions & 0 deletions test/ClangModules/MixedSource/mixed-target-using-module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ func testProtocolWrapper(conformer: ForwardClassUser) {
}
testProtocolWrapper(ProtoConformer())

func testDeclsNestedInObjCContainers() {
let _: NameInInterface = 0
let _: NameInProtocol = 0
let _: NameInCategory = 0
}