Skip to content

Commit 27fad7d

Browse files
committed
Clang importer: feed a Clang prepprocessor through to canInferDefaultArgument.
Omitting needless words while importing the full name function involves inference of default arguments, so we can get here while building Swift name lookup tables. It takes a lot of machinery to trigger this problem; tests forthcoming. There *must* be a better way to ensure this never happens, but it requires a bit more refactoring in the Clang importer.
1 parent b801eb9 commit 27fad7d

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,7 @@ auto ClangImporter::Implementation::importFullName(
24922492
// Objective-C methods.
24932493
if (auto method = dyn_cast<clang::ObjCMethodDecl>(D)) {
24942494
(void)omitNeedlessWordsInFunctionName(
2495+
clangSema.getPreprocessor(),
24952496
baseName,
24962497
argumentNames,
24972498
params,

lib/ClangImporter/ImportType.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,7 @@ OmissionTypeName ClangImporter::Implementation::getClangTypeNameForOmission(
17051705

17061706
/// Attempt to omit needless words from the given function name.
17071707
bool ClangImporter::Implementation::omitNeedlessWordsInFunctionName(
1708+
clang::Preprocessor &pp,
17081709
StringRef &baseName,
17091710
SmallVectorImpl<StringRef> &argumentNames,
17101711
ArrayRef<const clang::ParmVarDecl *> params,
@@ -1739,6 +1740,7 @@ bool ClangImporter::Implementation::omitNeedlessWordsInFunctionName(
17391740
// parameter.
17401741
bool hasDefaultArg
17411742
= canInferDefaultArgument(
1743+
pp,
17421744
param->getType(),
17431745
getParamOptionality(param,
17441746
!nonNullArgs.empty() && nonNullArgs[i],
@@ -1792,8 +1794,9 @@ clang::QualType ClangImporter::Implementation::getClangDeclContextType(
17921794
}
17931795

17941796
bool ClangImporter::Implementation::canInferDefaultArgument(
1795-
clang::QualType type, OptionalTypeKind clangOptionality,
1796-
Identifier baseName, unsigned numParams, bool isLastParameter) {
1797+
clang::Preprocessor &pp, clang::QualType type,
1798+
OptionalTypeKind clangOptionality, Identifier baseName,
1799+
unsigned numParams, bool isLastParameter) {
17971800
// Don't introduce a default argument for setters with only a single
17981801
// parameter.
17991802
if (numParams == 1 && camel_case::getFirstWord(baseName.str()) == "set")
@@ -1819,8 +1822,7 @@ bool ClangImporter::Implementation::canInferDefaultArgument(
18191822

18201823
// Option sets default to "[]" if they have "Options" in their name.
18211824
if (const clang::EnumType *enumTy = type->getAs<clang::EnumType>())
1822-
if (classifyEnum(getClangPreprocessor(), enumTy->getDecl())
1823-
== EnumKind::Options) {
1825+
if (classifyEnum(pp, enumTy->getDecl()) == EnumKind::Options) {
18241826
auto enumName = enumTy->getDecl()->getName();
18251827
for (auto word : reversed(camel_case::getWords(enumName))) {
18261828
if (camel_case::sameWordIgnoreFirstCase(word, "options"))
@@ -2135,7 +2137,8 @@ Type ClangImporter::Implementation::importMethodType(
21352137
if (InferDefaultArguments &&
21362138
(kind == SpecialMethodKind::Regular ||
21372139
kind == SpecialMethodKind::Constructor) &&
2138-
canInferDefaultArgument(param->getType(), optionalityOfParam,
2140+
canInferDefaultArgument(getClangPreprocessor(),
2141+
param->getType(), optionalityOfParam,
21392142
methodName.getBaseName(), numEffectiveParams,
21402143
isLastParameter)) {
21412144
defaultArg = DefaultArgumentKind::Normal;

lib/ClangImporter/ImporterImpl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
743743

744744
/// Omit needless words in a function name.
745745
bool omitNeedlessWordsInFunctionName(
746+
clang::Preprocessor &pp,
746747
StringRef &baseName,
747748
SmallVectorImpl<StringRef> &argumentNames,
748749
ArrayRef<const clang::ParmVarDecl *> params,
@@ -1125,7 +1126,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
11251126

11261127
/// Determine whether we can infer a default argument for a parameter with
11271128
/// the given \c type and (Clang) optionality.
1128-
bool canInferDefaultArgument(clang::QualType type,
1129+
bool canInferDefaultArgument(clang::Preprocessor &pp,
1130+
clang::QualType type,
11291131
OptionalTypeKind clangOptionality,
11301132
Identifier baseName,
11311133
unsigned numParams,

0 commit comments

Comments
 (0)