File indexing completed on 2024-05-19 05:41:59

0001 // ct_lvtclp_componentutil.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_componentutil.h>
0021 
0022 #include <ct_lvtmdb_componentobject.h>
0023 #include <ct_lvtmdb_objectstore.h>
0024 #include <ct_lvtmdb_packageobject.h>
0025 
0026 #include <filesystem>
0027 #include <memory>
0028 
0029 namespace Codethink::lvtclp {
0030 
0031 lvtmdb::ComponentObject *ComponentUtil::addComponent(const std::filesystem::path& filePath,
0032                                                      lvtmdb::PackageObject *parent,
0033                                                      lvtmdb::ObjectStore& memDb)
0034 // add or look up component for file
0035 {
0036     // special case for a mysterious file called "" that clang invents:
0037     if (filePath.empty()) {
0038         return nullptr;
0039     }
0040 
0041     const std::string name = filePath.stem().string();
0042     const std::string qualifiedName = [&]() {
0043         // Temporary solution to recognize lakosian packages
0044         {
0045             auto parentQualName = std::string{};
0046             parent->withROLock([&]() {
0047                 parentQualName = parent->qualifiedName();
0048             });
0049 
0050             auto contains = [](std::string const& str1, std::string const& str2) {
0051                 return str1.find(str2) != std::string::npos;
0052             };
0053 
0054             if (contains(parentQualName, "standalone/") || contains(parentQualName, "groups/")) {
0055                 return parentQualName + "/" + filePath.stem().string();
0056             }
0057         }
0058 
0059         return (filePath.parent_path() / name).string();
0060     }();
0061 
0062     return memDb.getOrAddComponent(qualifiedName, name, parent);
0063 }
0064 
0065 } // namespace Codethink::lvtclp