kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/tools/fyi/fyi.h (about) 1 /* 2 * Copyright 2015 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef KYTHE_CXX_TOOLS_FYI_FYI_H_ 18 #define KYTHE_CXX_TOOLS_FYI_FYI_H_ 19 20 #include "clang/Lex/PreprocessorOptions.h" 21 #include "clang/Tooling/Tooling.h" 22 #include "kythe/cxx/common/net_client.h" 23 24 namespace kythe { 25 namespace fyi { 26 27 class Action; 28 class FileTracker; 29 30 /// \brief Creates actions for fyi passes. 31 class ActionFactory : public clang::tooling::ToolAction { 32 public: 33 /// \param xrefs A source for cross-references. 34 /// \param iterations The maximum number of iterations to try before stopping. 35 ActionFactory(std::unique_ptr<XrefsClient> xrefs, size_t iterations); 36 ~ActionFactory(); 37 38 /// \brief Call before starting the next iteration around the fixpoint. 39 /// Decrements the number of iterations remaining. 40 /// \pre iterations > 0. 41 void BeginNextIteration(); 42 43 /// \brief Returns true if the tool should be run again as part of the 44 /// fixpoint loop; false if not. 45 bool ShouldRunAgain(); 46 47 /// \brief Returns the number of iterations we have left to run. 48 size_t iterations() const { return iterations_; } 49 50 /// \copydoc clang::tooling::ToolAction::runInvocation 51 bool runInvocation( 52 std::shared_ptr<clang::CompilerInvocation> invocation, 53 clang::FileManager* files, 54 std::shared_ptr<clang::PCHContainerOperations> pch_container_ops, 55 clang::DiagnosticConsumer* diagnostics) override; 56 57 private: 58 friend class Action; 59 60 /// \brief All of Clang's builtin headers. 61 std::vector<std::unique_ptr<llvm::MemoryBuffer>> builtin_headers_; 62 63 /// The client to use to find cross-references. 64 std::unique_ptr<XrefsClient> xrefs_; 65 66 /// \brief Try to find a `FileTracker` for the given path. 67 /// \param filename the path to search for 68 /// \return the old `FileTracker` for `filename`, or a new one. 69 FileTracker* GetOrCreateTracker(llvm::StringRef filename); 70 71 /// \brief Use the rewritten data stored in `file_trackers_` and the 72 /// builtin headers in `builtin_headers_` as input. 73 /// \param resource_dir The resource directory to use when mapping builtins. 74 /// \param remapped_buffers A vector of (path, buffer) to fill. It does 75 /// not gain ownership of the `llvm::MemoryBuffer` instances. 76 void RemapFiles(llvm::StringRef resource_dir, 77 std::vector<std::pair<std::string, llvm::MemoryBuffer*>>* 78 remapped_buffers); 79 80 /// \brief Maps paths to `FileTracker` instances. 81 typedef llvm::StringMap<FileTracker*> FileTrackerMap; 82 83 /// Maps full paths (as seen by Clang) to `FileTracker` instances. 84 FileTrackerMap file_trackers_; 85 86 /// How many more times should we run the main loop until we give up? 87 size_t iterations_; 88 }; 89 90 } // namespace fyi 91 } // namespace kythe 92 93 #endif // KYTHE_CXX_TOOLS_FYI_FYI_H_