Skip to content

Commit 16ec49d

Browse files
committed
Merge pull request #2502 from jckarter/clang-sema-lookupName
IRGen: Use clang::Sema::LookupName instead of TranslationUnit::lookup to find NSInteger.
2 parents cde9933 + f549269 commit 16ec49d

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace clang {
1919
class ASTContext;
2020
class Preprocessor;
21+
class Sema;
2122
} // namespace clang
2223

2324
namespace swift {
@@ -30,6 +31,7 @@ class ClangModuleLoader : public ModuleLoader {
3031
public:
3132
virtual clang::ASTContext &getClangASTContext() const = 0;
3233
virtual clang::Preprocessor &getClangPreprocessor() const = 0;
34+
virtual clang::Sema &getClangSema() const = 0;
3335
virtual void printStatistics() const = 0;
3436

3537
/// Returns the module that contains imports and declarations from all loaded

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class ClangImporter final : public ClangModuleLoader {
222222
clang::TargetInfo &getTargetInfo() const;
223223
clang::ASTContext &getClangASTContext() const override;
224224
clang::Preprocessor &getClangPreprocessor() const override;
225-
clang::Sema &getClangSema() const;
225+
clang::Sema &getClangSema() const override;
226226
clang::CodeGenOptions &getClangCodeGenOpts() const;
227227

228228
std::string getClangModuleHash() const;

lib/IRGen/GenClangType.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "clang/AST/Decl.h"
2929
#include "clang/AST/DeclObjC.h"
3030
#include "clang/AST/Type.h"
31+
#include "clang/Sema/Sema.h"
3132
#include "clang/Basic/TargetInfo.h"
3233
#include "IRGenModule.h"
3334

@@ -228,18 +229,14 @@ clang::CanQualType GenClangType::visitStructType(CanStructType type) {
228229
}
229230

230231
static clang::CanQualType getClangBuiltinTypeFromTypedef(
231-
const clang::ASTContext &context, StringRef typedefName) {
232-
auto identifier = &context.Idents.get(typedefName);
233-
auto lookup = context.getTranslationUnitDecl()->lookup(identifier);
232+
clang::Sema &sema, StringRef typedefName) {
233+
auto &context = sema.getASTContext();
234234

235-
clang::TypedefDecl *typedefDecl = nullptr;
236-
for (auto found : lookup) {
237-
auto foundTypedef = dyn_cast<clang::TypedefDecl>(found);
238-
if (!foundTypedef)
239-
continue;
240-
typedefDecl = foundTypedef;
241-
break;
242-
}
235+
auto identifier = &context.Idents.get(typedefName);
236+
auto found = sema.LookupSingleName(sema.TUScope, identifier,
237+
clang::SourceLocation(),
238+
clang::Sema::LookupOrdinaryName);
239+
auto typedefDecl = dyn_cast_or_null<clang::TypedefDecl>(found);
243240
if (!typedefDecl)
244241
return {};
245242

@@ -287,17 +284,18 @@ ClangTypeConverter::reverseBuiltinTypeMapping(IRGenModule &IGM,
287284
CanType swiftType = getNamedSwiftType(stdlib, swiftName);
288285
if (!swiftType) return;
289286

287+
auto &sema = IGM.Context.getClangModuleLoader()->getClangSema();
290288
// Handle Int and UInt specially. On Apple platforms, these correspond to
291289
// the NSInteger and NSUInteger typedefs, so map them back to those typedefs
292290
// if they're available, to ensure we get consistent ObjC @encode strings.
293291
if (swiftType->getAnyNominal() == IGM.Context.getIntDecl()) {
294-
if (auto NSIntegerTy = getClangBuiltinTypeFromTypedef(ctx, "NSInteger")) {
292+
if (auto NSIntegerTy = getClangBuiltinTypeFromTypedef(sema, "NSInteger")){
295293
Cache.insert({swiftType, NSIntegerTy});
296294
return;
297295
}
298296
} else if (swiftType->getAnyNominal() == IGM.Context.getUIntDecl()) {
299297
if (auto NSUIntegerTy =
300-
getClangBuiltinTypeFromTypedef(ctx, "NSUInteger")) {
298+
getClangBuiltinTypeFromTypedef(sema, "NSUInteger")) {
301299
Cache.insert({swiftType, NSUIntegerTy});
302300
return;
303301
}

0 commit comments

Comments
 (0)