@@ -2009,13 +2009,49 @@ considerErrorImport(ClangImporter::Implementation &importer,
2009
2009
return None;
2010
2010
}
2011
2011
2012
- // / Determine whether we are allowed to strip the module prefix from
2013
- // / an entity with the given name.
2014
- static bool canStripModulePrefix (StringRef name) {
2015
- if (auto known = getKnownFoundationEntity (name))
2016
- return !nameConflictsWithStandardLibrary (*known);
2017
-
2018
- return true ;
2012
+ // Determine whether we should strip the module prefix from a global-scope
2013
+ // entity in the given module and with the given base name.
2014
+ static unsigned stripModulePrefixLength (
2015
+ const llvm::StringMap<std::string> &modulePrefixes,
2016
+ StringRef moduleName,
2017
+ StringRef baseName) {
2018
+ // Do we have a module prefix for this module?
2019
+ auto prefixPos = modulePrefixes.find (moduleName);
2020
+ if (prefixPos == modulePrefixes.end ()) return 0 ;
2021
+
2022
+ // Is the prefix actually there?
2023
+ if (!baseName.startswith (prefixPos->second )) return 0 ;
2024
+
2025
+ // Check whether this is a known Foundation entity that conflicts with the
2026
+ // standard library.
2027
+ if (auto known = getKnownFoundationEntity (baseName))
2028
+ if (nameConflictsWithStandardLibrary (*known))
2029
+ return 0 ;
2030
+
2031
+ // If the character following the prefix is a '_', eat that, too.
2032
+ unsigned prefixLen = prefixPos->second .size ();
2033
+ if (prefixLen < baseName.size () && baseName[prefixLen] == ' _' )
2034
+ ++prefixLen;
2035
+
2036
+ // Make sure we're left with an identifier.
2037
+ if (prefixLen == baseName.size () ||
2038
+ !Lexer::isIdentifier (baseName.substr (prefixLen)))
2039
+ return 0 ;
2040
+
2041
+ // We can strip the module prefix.
2042
+ return prefixLen;
2043
+ }
2044
+
2045
+ // / Determine whether we should lowercase the first word of the given value
2046
+ // / name.
2047
+ static bool shouldLowercaseValueName (StringRef name) {
2048
+ // If we see any lowercase characters, we can lowercase.
2049
+ for (auto c : name) {
2050
+ if (clang::isLowercase (c)) return true ;
2051
+ }
2052
+
2053
+ // Otherwise, lowercasing will either be a no-op or we have ALL_CAPS.
2054
+ return false ;
2019
2055
}
2020
2056
2021
2057
// / Returns true if it is expected that the macro is ignored.
@@ -2529,15 +2565,13 @@ auto ClangImporter::Implementation::importFullName(
2529
2565
moduleName = module ->getTopLevelModuleName ();
2530
2566
else
2531
2567
moduleName = owningD->getASTContext ().getLangOpts ().CurrentModule ;
2532
- auto prefixPos = ModulePrefixes.find (moduleName);
2533
- if (prefixPos != ModulePrefixes.end () &&
2534
- canStripModulePrefix (baseName) &&
2535
- baseName.startswith (prefixPos->second )) {
2568
+ if (unsigned prefixLen = stripModulePrefixLength (ModulePrefixes,
2569
+ moduleName, baseName)) {
2536
2570
// Strip off the prefix.
2537
- baseName = baseName.substr (prefixPos-> second . size () );
2571
+ baseName = baseName.substr (prefixLen );
2538
2572
2539
2573
// If the result is a value, lowercase it.
2540
- if (isa<clang::ValueDecl>(D))
2574
+ if (isa<clang::ValueDecl>(D) && shouldLowercaseValueName (baseName) )
2541
2575
baseName = camel_case::toLowercaseWord (baseName,
2542
2576
omitNeedlessWordsScratch);
2543
2577
}
0 commit comments