77// ===----------------------------------------------------------------------===//
88
99#include " QueryAPIMapping.h"
10+ #include " llvm/ADT/Twine.h"
1011#include " llvm/Support/raw_ostream.h"
1112#include < unordered_map>
1213
@@ -27,34 +28,32 @@ void APIMapping::registerEntry(std::string Name, llvm::StringRef SourceCode) {
2728 const auto TargetIndex = EntryArray.size ();
2829 EntryMap[Name] = TargetIndex; // Set the entry whether it exist or not.
2930 // Try to fuzz the original API name (only when the entry not exist):
30- // 1. Remove partial or all leading '_'.
31- // 2. For each name got by step 1, put 4 kind of fuzzed name into the map
31+ // 1. Change "Name" to lower case. (Querying will change "Key" to lower too)
32+ // 2. Remove partial or all suffix '_'.
33+ std::transform (Name.begin (), Name.end (), Name.begin (), ::tolower);
34+ while (Name.back () == ' _' ) {
35+ Name.erase (Name.end () - 1 );
36+ EntryMap.try_emplace (Name, TargetIndex);
37+ }
38+ const auto EmplaceWithAndWithoutSuffix = [TargetIndex](
39+ llvm::StringRef Name,
40+ llvm::StringRef Suffix) {
41+ EntryMap.try_emplace (Name.str (), TargetIndex);
42+ if (Name.take_back (Suffix.size ()) == Suffix) {
43+ EntryMap.try_emplace (Name.drop_back (Suffix.size ()).str (), TargetIndex);
44+ } else {
45+ EntryMap.try_emplace (llvm::Twine (Name).concat (Suffix).str (), TargetIndex);
46+ }
47+ };
48+ // 3. Remove partial or all leading '_'.
49+ // 4. For each name got by step 1, put 2 kind of fuzzed name into the map
3250 // keys:
3351 // (1) original name
3452 // (2) remove or add Suffix "_v2"
35- // (3) first char upper case name
36- // (4) all char upper case name
37- // (5) all char lower case name
38- for (int i = Name.find_first_not_of (" _" ); i >= 0 ; --i) {
39- auto TempName = Name;
40- std::string Suffix = " _v2" ;
41- if (TempName.size () > Suffix.length () &&
42- TempName.substr (TempName.size () - Suffix.length ()) == Suffix) {
43- EntryMap.try_emplace (TempName.substr (0 , TempName.size () - 3 ),
44- TargetIndex);
45- } else {
46- EntryMap.try_emplace (TempName + Suffix, TargetIndex);
47- }
48- TempName[i] = std::toupper (TempName[i]);
49- EntryMap.try_emplace (TempName, TargetIndex);
50- std::transform (TempName.begin (), TempName.end (), TempName.begin (),
51- ::toupper);
52- EntryMap.try_emplace (TempName, TargetIndex);
53- std::transform (TempName.begin (), TempName.end (), TempName.begin (),
54- ::tolower);
55- EntryMap.try_emplace (TempName, TargetIndex);
53+ EmplaceWithAndWithoutSuffix (Name, " _v2" );
54+ while (Name.front () == ' _' ) {
5655 Name.erase (0 , 1 );
57- EntryMap. try_emplace (Name, TargetIndex );
56+ EmplaceWithAndWithoutSuffix (Name, " _v2 " );
5857 }
5958 EntryArray.emplace_back (SourceCode);
6059}
@@ -68,6 +67,10 @@ llvm::StringRef APIMapping::getAPISourceCode(std::string Key) {
6867 Key.erase (Key.find_last_not_of (" " ) + 1 );
6968 auto Iter = EntryMap.find (Key);
7069 if (Iter == EntryMap.end ()) {
70+ if (Key.find (' <' ) != std::string::npos ||
71+ Key.find (' >' ) != std::string::npos) {
72+ Key = " kernel" ;
73+ }
7174 std::transform (Key.begin (), Key.end (), Key.begin (), ::tolower);
7275 Iter = EntryMap.find (Key);
7376 }
0 commit comments