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

0001 // ct_lvtclp_staticfnhandler.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_STATICFNHANDLER
0021 #define INCLUDED_CT_LVTCLP_STATICFNHANDLER
0022 
0023 //@PURPOSE: Temporary storage of which classes use each static function and
0024 //          what relationships that implies. This should be re-created for each
0025 //          file
0026 //
0027 //@CLASSES:
0028 //  lvtclp::StaticFnHandler Not for use outside lvtclp
0029 //
0030 //@SEE_ALSO: lvtclp::CodebaseDbVisitor
0031 //
0032 //@DESCRIPTION:
0033 //  It is an implementation detail whether a class method references something
0034 //  directly or via a static free function. Methods from multiple classes might
0035 //  use the same static free function, in which case all of those classes should
0036 //  get the uses-in-the-implementation relationships implied by the free
0037 //  function. It would not be appropriate to add file scope free functions to
0038 //  the database and so we should cache all of the information here then add
0039 //  the database realtionships at the end of AST processing for a particular
0040 //  translation unit.
0041 
0042 #include <clang/AST/Decl.h>
0043 
0044 #include <memory>
0045 
0046 namespace Codethink::lvtmdb {
0047 class ObjectStore;
0048 }
0049 namespace Codethink::lvtmdb {
0050 class TypeObject;
0051 }
0052 namespace Codethink::lvtmdb {
0053 class FunctionObject;
0054 }
0055 
0056 namespace Codethink::lvtclp {
0057 
0058 // ==============================
0059 // class StaticFnHandler
0060 // ==============================
0061 
0062 class StaticFnHandler {
0063     // Each instance should be unique to a given file because we allow static
0064     // functions
0065 
0066     // PRIVATE TYPES
0067     struct Private;
0068 
0069     // DATA
0070     std::unique_ptr<Private> d;
0071 
0072   public:
0073     // CREATORS
0074     explicit StaticFnHandler(lvtmdb::ObjectStore& memDb);
0075 
0076     ~StaticFnHandler() noexcept;
0077 
0078     // MANIPULATORS
0079     void addFnUses(const clang::FunctionDecl *decl, lvtmdb::TypeObject *udt);
0080     // Function described by decl uses-in-the-impl udt
0081     void addFnUses(const clang::FunctionDecl *decl, const clang::FunctionDecl *dep);
0082     // Function described by decl uses-in-the-impl dep
0083     void addUdtUses(lvtmdb::TypeObject *udt, const clang::FunctionDecl *decl);
0084     // udt uses-in-the-impl function described by decl
0085     void addCallgraphDep(lvtmdb::FunctionObject *source_f, lvtmdb::FunctionObject *target_f);
0086 
0087     void writeOutToDb();
0088     // (Once traversal of a translation unit is done) write out the
0089     // relationships to the database
0090 
0091     void reset();
0092     // Clear all saved state (except the database session pointer)
0093 
0094   private:
0095     // MANIPULATORS
0096     void flattenFn(std::size_t sourceFn);
0097 };
0098 
0099 } // end namespace Codethink::lvtclp
0100 
0101 #endif