1 /* 2 * Copyright 2010-2012, The Android Open Source Project 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 BCC_SOURCE_H 18 #define BCC_SOURCE_H 19 20 #include <string> 21 22 namespace llvm { 23 class Module; 24 } 25 26 namespace bcinfo { 27 class MetadataExtractor; 28 } 29 30 namespace bcc { 31 32 class BCCContext; 33 34 class Source { 35 private: 36 const std::string mName; // A unique name 37 BCCContext &mContext; 38 llvm::Module *mModule; 39 40 bcinfo::MetadataExtractor *mMetadata; 41 42 // If true, destructor won't destroy the mModule. 43 bool mNoDelete; 44 45 // Keep track of whether mModule is destroyed (possibly as a consequence of 46 // getting linked with a different llvm::Module). 47 bool mIsModuleDestroyed; 48 49 private: 50 Source(const char* name, BCCContext &pContext, llvm::Module &pModule, 51 bool pNoDelete = false); 52 53 public: 54 static Source *CreateFromBuffer(BCCContext &pContext, 55 const char *pName, 56 const char *pBitcode, 57 size_t pBitcodeSize); 58 59 static Source *CreateFromFile(BCCContext &pContext, 60 const std::string &pPath); 61 62 // Create a Source object from an existing module. If pNoDelete 63 // is true, destructor won't call delete on the given module. 64 static Source *CreateFromModule(BCCContext &pContext, 65 const char* name, 66 llvm::Module &pModule, 67 bool pNoDelete = false); 68 69 static Source *CreateEmpty(BCCContext &pContext, const std::string &pName); 70 getName()71 const std::string& getName() const { return mName; } 72 73 // Merge the current source with pSource. pSource 74 // will be destroyed after successfully merged. Return false on error. 75 bool merge(Source &pSource); 76 getContext()77 inline BCCContext &getContext() 78 { return mContext; } getContext()79 inline const BCCContext &getContext() const 80 { return mContext; } 81 82 void setModule(llvm::Module *pModule); 83 getModule()84 inline llvm::Module &getModule() 85 { return *mModule; } getModule()86 inline const llvm::Module &getModule() const 87 { return *mModule; } 88 89 // Get the "identifier" of the bitcode. This will return the value of pName 90 // when it's created using CreateFromBuffer and pPath if CreateFromFile(). 91 const std::string &getIdentifier() const; 92 93 void addBuildChecksumMetadata(const char *) const; 94 95 // Get whether debugging has been enabled for this module by checking 96 // for presence of debug info in the module. 97 bool getDebugInfoEnabled() const; 98 99 // Extract metadata from mModule using MetadataExtractor. 100 bool extractMetadata(); getMetadata()101 bcinfo::MetadataExtractor* getMetadata() const { return mMetadata; } 102 103 // Mark mModule was destroyed in the process of linking with a different 104 // llvm::Module markModuleDestroyed()105 void markModuleDestroyed() { mIsModuleDestroyed = true; } 106 107 ~Source(); 108 }; 109 110 } // namespace bcc 111 112 #endif // BCC_SOURCE_H 113