File indexing completed on 2024-05-19 05:42:06
0001 // ct_lvtclp_visitlog.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 0020 #include <ct_lvtclp_visitlog.h> 0021 0022 #include <clang/Basic/SourceLocation.h> 0023 0024 #include <unordered_set> 0025 0026 namespace { 0027 0028 struct CallbackId { 0029 clang::SourceLocation location; 0030 clang::Decl::Kind declKind; 0031 int templateKind; 0032 // We can't store the clang enum here because different Decl kinds have 0033 // different template enums: e.g. FunctionDecl::TemplatedKind 0034 0035 explicit CallbackId(const clang::Decl *decl, const clang::Decl::Kind kind, const int templateKind): 0036 location(decl->getLocation()), declKind(kind), templateKind(templateKind) 0037 { 0038 } 0039 }; 0040 0041 inline bool operator==(const CallbackId& lhs, const CallbackId& rhs) 0042 { 0043 return lhs.declKind == rhs.declKind && lhs.templateKind == rhs.templateKind && lhs.location == rhs.location; 0044 } 0045 0046 } // namespace 0047 0048 namespace std { 0049 template<> 0050 struct hash<CallbackId> 0051 // Implementation of std::hash for CallbackId 0052 { 0053 std::size_t operator()(const CallbackId& id) const noexcept 0054 { 0055 const unsigned loc = id.location.getRawEncoding(); 0056 const unsigned kind = id.declKind; 0057 0058 // Hash will be composed of: 0059 // [32bit loc] [16bit kind] [16bit templateKind] 0060 size_t hash = 0; 0061 hash |= static_cast<size_t>(loc) << 32; 0062 hash |= (static_cast<size_t>(kind) & 0x000000ff) << 16; 0063 hash |= static_cast<size_t>(id.templateKind) & 0x000000ff; 0064 0065 return hash; 0066 } 0067 }; 0068 } // namespace std 0069 0070 namespace Codethink::lvtclp { 0071 0072 struct VisitLog::Private { 0073 std::unordered_set<CallbackId> visited; 0074 }; 0075 0076 VisitLog::VisitLog(): d(std::make_unique<VisitLog::Private>()) 0077 { 0078 } 0079 0080 VisitLog::~VisitLog() noexcept = default; 0081 0082 bool VisitLog::alreadyVisited(const clang::Decl *decl, const clang::Decl::Kind kind, const int templateKind) 0083 { 0084 auto [_, constructed] = d->visited.emplace(decl, kind, templateKind); 0085 return !constructed; 0086 } 0087 0088 } // end namespace Codethink::lvtclp