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

0001 // lvtclp_headercallbacks.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_headercallbacks.h>
0021 
0022 #include <ct_lvtclp_clputil.h>
0023 
0024 #include <ct_lvtmdb_componentobject.h>
0025 #include <ct_lvtmdb_fileobject.h>
0026 #include <ct_lvtmdb_objectstore.h>
0027 #include <ct_lvtmdb_packageobject.h>
0028 
0029 #include <clang/Basic/FileManager.h>
0030 #include <clang/Basic/Version.h>
0031 
0032 #include <filesystem>
0033 #include <utility>
0034 
0035 namespace Codethink::lvtclp {
0036 
0037 HeaderCallbacks::HeaderCallbacks(clang::SourceManager *sm,
0038                                  lvtmdb::ObjectStore& memDb,
0039                                  std::filesystem::path prefix,
0040                                  std::vector<std::filesystem::path> nonLakosians,
0041                                  std::vector<std::pair<std::string, std::string>> thirdPartyDirs,
0042                                  std::vector<llvm::GlobPattern> ignoreGlobs,
0043                                  std::optional<HeaderLocationCallback_f> headerLocationCallback):
0044     sourceManager(*sm),
0045     d_memDb(memDb),
0046     d_prefix(std::filesystem::weakly_canonical(prefix)),
0047     d_nonLakosianDirs(std::move(nonLakosians)),
0048     d_thirdPartyDirs(std::move(thirdPartyDirs)),
0049     d_ignoreGlobs(std::move(ignoreGlobs)),
0050     d_headerLocationCallback(std::move(headerLocationCallback))
0051 {
0052 }
0053 
0054 void HeaderCallbacks::InclusionDirective(clang::SourceLocation HashLoc,
0055                                          const clang::Token& IncludeTok,
0056                                          clang::StringRef FileName,
0057                                          bool IsAngled,
0058                                          clang::CharSourceRange FilenameRange,
0059                                          clang::OptionalFileEntryRef File,
0060                                          clang::StringRef SearchPath,
0061                                          clang::StringRef RelativePath,
0062                                          const clang::Module *Imported,
0063                                          clang::SrcMgr::CharacteristicKind FileType)
0064 {
0065     if (!File.has_value()) {
0066         return;
0067     }
0068 
0069     auto realPathStr = File.value().getFileEntry().tryGetRealPathName().str();
0070 
0071     if (d_sourceFile_p == nullptr) {
0072         return;
0073     }
0074 
0075     if (ClpUtil::isFileIgnored(std::filesystem::path{realPathStr}.filename().string(), d_ignoreGlobs)) {
0076         return;
0077     }
0078 
0079     lvtmdb::FileObject *filePtr =
0080         ClpUtil::writeSourceFile(realPathStr, true, d_memDb, d_prefix, d_nonLakosianDirs, d_thirdPartyDirs);
0081 
0082     if (filePtr == d_sourceFile_p) {
0083         return;
0084     }
0085 
0086     if (d_headerLocationCallback) {
0087         auto sourceFile = std::string{};
0088         d_sourceFile_p->withROLock([&]() {
0089             sourceFile = d_sourceFile_p->name();
0090         });
0091         auto includedFile = FileName.str();
0092         auto lineNo = sourceManager.getExpansionLineNumber(HashLoc);
0093         (*d_headerLocationCallback)(sourceFile, includedFile, lineNo);
0094     }
0095 
0096     // add include relationship d_sourceFile_p -> filePtr
0097     assert(filePtr);
0098     assert(d_sourceFile_p);
0099     lvtmdb::FileObject::addIncludeRelation(d_sourceFile_p, filePtr);
0100 
0101     lvtmdb::ComponentObject *sourceComp = nullptr;
0102     lvtmdb::PackageObject *sourcePkg = nullptr;
0103     d_sourceFile_p->withROLock([&sourceComp, &sourcePkg, this]() {
0104         sourceComp = d_sourceFile_p->component();
0105         sourcePkg = d_sourceFile_p->package();
0106     });
0107 
0108     lvtmdb::ComponentObject *targetComp = nullptr;
0109     lvtmdb::PackageObject *targetPkg = nullptr;
0110     filePtr->withROLock([&targetComp, &targetPkg, &filePtr]() {
0111         targetComp = filePtr->component();
0112         targetPkg = filePtr->package();
0113     });
0114 
0115     if (sourceComp && targetComp && sourceComp != targetComp) {
0116         lvtmdb::ComponentObject::addDependency(sourceComp, targetComp);
0117     }
0118 
0119     if (sourcePkg && targetPkg && sourcePkg != targetPkg) {
0120         lvtmdb::PackageObject::addDependency(sourcePkg, targetPkg);
0121 
0122         lvtmdb::PackageObject *sourceParent = nullptr;
0123         lvtmdb::PackageObject *targetParent = nullptr;
0124         sourcePkg->withROLock([&sourcePkg, &sourceParent]() {
0125             sourceParent = sourcePkg->parent();
0126         });
0127         targetPkg->withROLock([&targetPkg, &targetParent]() {
0128             targetParent = targetPkg->parent();
0129         });
0130 
0131         if (sourceParent == nullptr) {
0132             // Source package is a standalone package. It is necessary to register the dependency to the package group
0133             if (targetParent != nullptr) {
0134                 lvtmdb::PackageObject::addDependency(sourcePkg, targetParent);
0135             }
0136         }
0137 
0138         if (targetParent == nullptr) {
0139             // Target package is a standalone package. It is necessary to register the dependency from the package group
0140             if (sourceParent != nullptr) {
0141                 lvtmdb::PackageObject::addDependency(sourceParent, targetPkg);
0142             }
0143         }
0144     }
0145 }
0146 
0147 void HeaderCallbacks::FileChanged(clang::SourceLocation sourceLocation,
0148                                   FileChangeReason reason,
0149                                   clang::SrcMgr::CharacteristicKind fileType,
0150                                   clang::FileID prevFID)
0151 {
0152     const std::string realPath = ClpUtil::getRealPath(sourceLocation, sourceManager);
0153 
0154     if (ClpUtil::isFileIgnored(std::filesystem::path{realPath}.filename().string(), d_ignoreGlobs)) {
0155         d_sourceFile_p = nullptr;
0156         return;
0157     }
0158 
0159     const FileType type = ClpUtil::categorisePath(realPath);
0160     bool isHeader = false;
0161     if (type == FileType::e_Header) {
0162         isHeader = true;
0163     }
0164 
0165     d_sourceFile_p =
0166         ClpUtil::writeSourceFile(realPath, isHeader, d_memDb, d_prefix, d_nonLakosianDirs, d_thirdPartyDirs);
0167 }
0168 
0169 } // end namespace Codethink::lvtclp