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

0001 // lvtclp_headercallbacks.h                                           -*-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 #ifndef INCLUDED_CT_LVTCLP_HEADERCALLBACKS
0021 #define INCLUDED_CT_LVTCLP_HEADERCALLBACKS
0022 
0023 //@PURPOSE: Add files to the database as they are touched by clang
0024 //
0025 //@CLASSES: lvtclp::HeaderCallbacks Implements clang::PPCallbacks
0026 //
0027 //@SEE_ALSO: clang::PPCallbacks
0028 
0029 #include <ct_lvtmdb_fileobject.h>
0030 #include <ct_lvtmdb_objectstore.h>
0031 
0032 // std
0033 #include <filesystem>
0034 #include <functional>
0035 #include <llvm/Support/GlobPattern.h>
0036 #include <memory>
0037 #include <optional>
0038 #include <vector>
0039 
0040 // llvm
0041 #include <llvm/Support/CommandLine.h>
0042 
0043 // clang
0044 #include <clang/Basic/Version.h>
0045 #include <clang/Lex/PPCallbacks.h>
0046 
0047 // Qt
0048 #include <QString>
0049 
0050 namespace Codethink {
0051 
0052 // FORWARD DECLARATIONS
0053 
0054 namespace lvtclp {
0055 
0056 // =====================
0057 // class HeaderCallbacks
0058 // =====================
0059 
0060 class HeaderCallbacks : public clang::PPCallbacks {
0061   public:
0062     using HeaderLocationCallback_f =
0063         std::function<void(std::string const& sourceFile, std::string const& includedFile, unsigned lineNo)>;
0064 
0065   private:
0066     // Implements clang::PPCallbacks. These callbacks make sure that new files
0067     // are added to the database as they are processed by clang
0068     // Not for use outside lvtclp
0069 
0070     // DATA
0071     lvtmdb::FileObject *d_sourceFile_p = nullptr;
0072     // Stores the source file currently processed by clang so that we know
0073     // where to add includes
0074 
0075     clang::SourceManager& sourceManager;
0076     // Clang context providing the filename
0077 
0078     lvtmdb::ObjectStore& d_memDb;
0079     // The active in-memory database session
0080 
0081     std::filesystem::path d_prefix;
0082 
0083     std::vector<std::filesystem::path> d_nonLakosianDirs;
0084     std::vector<std::pair<std::string, std::string>> d_thirdPartyDirs;
0085     std::vector<llvm::GlobPattern> d_ignoreGlobs;
0086     std::optional<HeaderLocationCallback_f> d_headerLocationCallback;
0087 
0088   public:
0089     // CREATORS
0090     HeaderCallbacks(clang::SourceManager *sm,
0091                     lvtmdb::ObjectStore& memDb,
0092                     std::filesystem::path prefix,
0093                     std::vector<std::filesystem::path> nonLakosians,
0094                     std::vector<std::pair<std::string, std::string>> thirdPartyDirs,
0095                     std::vector<llvm::GlobPattern> ignoreGlobs,
0096                     std::optional<HeaderLocationCallback_f> headerLocationCallback = std::nullopt);
0097 
0098     // MANIPULATORS
0099     void InclusionDirective(clang::SourceLocation HashLoc,
0100                             const clang::Token& IncludeTok,
0101                             clang::StringRef FileName,
0102                             bool IsAngled,
0103                             clang::CharSourceRange FilenameRange,
0104                             clang::OptionalFileEntryRef File,
0105                             clang::StringRef SearchPath,
0106                             clang::StringRef RelativePath,
0107                             const clang::Module *Imported,
0108                             clang::SrcMgr::CharacteristicKind FileType) override;
0109     // Invoked when clang processes an #include directive
0110 
0111     void FileChanged(clang::SourceLocation sourceLocation,
0112                      FileChangeReason reason,
0113                      clang::SrcMgr::CharacteristicKind fileType,
0114                      clang::FileID prevFID) override;
0115     // Invoked whenever a source file is entered or exited by clang
0116 };
0117 
0118 } // namespace lvtclp
0119 
0120 } // end namespace Codethink
0121 
0122 #endif