Skip to content

Commit f549269

Browse files
committed
IRGen: Use clang::Sema::LookupName instead of TranslationUnit::lookup to find NSInteger.
Sema's lookup is much more efficient.
1 parent 2210eff commit f549269

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

@@ -226,18 +227,14 @@ clang::CanQualType GenClangType::visitStructType(CanStructType type) {
226227
}
227228

228229
static clang::CanQualType getClangBuiltinTypeFromTypedef(
229-
const clang::ASTContext &context, StringRef typedefName) {
230-
auto identifier = &context.Idents.get(typedefName);
231-
auto lookup = context.getTranslationUnitDecl()->lookup(identifier);
230+
clang::Sema &sema, StringRef typedefName) {
231+
auto &context = sema.getASTContext();
232232

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

@@ -285,17 +282,18 @@ ClangTypeConverter::reverseBuiltinTypeMapping(IRGenModule &IGM,
285282
CanType swiftType = getNamedSwiftType(stdlib, swiftName);
286283
if (!swiftType) return;
287284

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

0 commit comments

Comments
 (0)