File indexing completed on 2024-05-19 05:42:04

0001 // ct_lvtclp_pysicaldepscanner.t.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_lvtmdb_functionobject.h>
0021 #include <ct_lvtmdb_methodobject.h>
0022 #include <ct_lvtmdb_namespaceobject.h>
0023 #include <ct_lvtmdb_objectstore.h>
0024 #include <ct_lvtmdb_typeobject.h>
0025 
0026 #include <ct_lvtclp_physicaldepscanner.h>
0027 #include <ct_lvtclp_testutil.h>
0028 #include <ct_lvtclp_toolexecutor.h>
0029 
0030 #include <memory>
0031 #include <unordered_set>
0032 
0033 // Auto generated by CMake / see main CMakeLists file, and
0034 // the CMakeLists on this folder.
0035 #include <catch2-local-includes.h>
0036 #include <test-project-paths.h>
0037 
0038 using namespace Codethink::lvtclp;
0039 using namespace Codethink::lvtmdb;
0040 using namespace Codethink;
0041 
0042 const PyDefaultGilReleasedContext defaultGilContextForTesting;
0043 
0044 struct FoundIncludeTestData {
0045     struct HashFunc {
0046         size_t operator()(const FoundIncludeTestData& data) const
0047         {
0048             return std::hash<std::string>{}(data.sourceFile) ^ std::hash<std::string>{}(data.includedFile)
0049                 ^ std::hash<unsigned>{}(data.lineNo);
0050         }
0051     };
0052 
0053     std::string sourceFile;
0054     std::string includedFile;
0055     unsigned lineNo;
0056 
0057     bool operator==(const FoundIncludeTestData& other) const
0058     {
0059         return this->sourceFile == other.sourceFile && this->includedFile == other.includedFile
0060             && this->lineNo == other.lineNo;
0061     }
0062 
0063     friend auto operator<<(std::ostream& os, FoundIncludeTestData const& self) -> std::ostream&
0064     {
0065         return os << self.sourceFile << " includes " << self.includedFile << " on line " << self.lineNo;
0066     }
0067 };
0068 
0069 TEST_CASE("Optional include location callbacks")
0070 {
0071     auto const PREFIX = std::string{TEST_PRJ_PATH};
0072 
0073     auto const prjAPath = PREFIX + "/project_with_includes_outside_src/prjA";
0074     auto const prjBPath = PREFIX + "/project_with_includes_outside_src/prjB";
0075     auto cdb =
0076         StaticCompilationDatabase{{{"groups/one/oneaaa/oneaaa_comp.cpp", "oneaaa_comp.o"}},
0077                                   "placeholder",
0078                                   {"-I" + prjAPath + "/groups/one/oneaaa/", "-I" + prjBPath + "/groups/two/twoaaa/"},
0079                                   prjAPath};
0080     auto memDb = lvtmdb::ObjectStore{};
0081 
0082     auto executor = ToolExecutor{cdb, 1, [](auto&& _1, auto&& _2) {}, memDb};
0083 
0084     auto foundIncludes = std::unordered_set<FoundIncludeTestData, FoundIncludeTestData::HashFunc>{};
0085     auto headerLocationCallback = [&](std::string const& sourceFile, std::string const& includedFile, unsigned lineNo) {
0086         foundIncludes.insert(FoundIncludeTestData{sourceFile, includedFile, lineNo});
0087     };
0088     auto err = executor.execute(std::make_unique<DepScanActionFactory>(
0089         memDb,
0090         PREFIX,
0091         std::vector<std::filesystem::path>{},
0092         std::vector<std::pair<std::string, std::string>>{},
0093         [](auto&& _) {},
0094         std::vector<llvm::GlobPattern>{},
0095         headerLocationCallback));
0096 
0097     REQUIRE(!err);
0098     REQUIRE(foundIncludes.size() == 2);
0099     REQUIRE(foundIncludes.find(FoundIncludeTestData{"oneaaa_comp.h", "twoaaa_comp.h", 5}) != foundIncludes.end());
0100     REQUIRE(foundIncludes.find(FoundIncludeTestData{"oneaaa_comp.cpp", "oneaaa_comp.h", 2}) != foundIncludes.end());
0101 }