LLVM API Documentation

IncludeFile.h

Go to the documentation of this file.
00001 //===- llvm/System/IncludeFile.h - Ensure Linking Of Library ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the FORCE_DEFINING_FILE_TO_BE_LINKED and DEFINE_FILE_FOR
00011 // macros.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SYSTEM_INCLUDEFILE_H
00016 #define LLVM_SYSTEM_INCLUDEFILE_H
00017 
00018 /// This macro is the public interface that IncludeFile.h exports. This gives
00019 /// us the option to implement the "link the definition" capability in any 
00020 /// manner that we choose. All header files that depend on a specific .cpp
00021 /// file being linked at run time should use this macro instead of the
00022 /// IncludeFile class directly. 
00023 /// 
00024 /// For example, foo.h would use:<br/>
00025 /// <tt>FORCE_DEFINING_FILE_TO_BE_LINKED(foo)</tt><br/>
00026 /// 
00027 /// And, foo.cp would use:<br/>
00028 /// <tt>DEFINING_FILE_FOR(foo)</tt><br/>
00029 #ifdef __GNUC__
00030 // If the `used' attribute is available, use it to create a variable
00031 // with an initializer that will force the linking of the defining file.
00032 #define FORCE_DEFINING_FILE_TO_BE_LINKED(name) \
00033   namespace llvm { \
00034     extern const char name ## LinkVar; \
00035     __attribute__((used)) static const char *const name ## LinkObj = \
00036       &name ## LinkVar; \
00037   } 
00038 #else
00039 // Otherwise use a constructor call.
00040 #define FORCE_DEFINING_FILE_TO_BE_LINKED(name) \
00041   namespace llvm { \
00042     extern const char name ## LinkVar; \
00043     static const IncludeFile name ## LinkObj ( &name ## LinkVar ); \
00044   } 
00045 #endif
00046 
00047 /// This macro is the counterpart to FORCE_DEFINING_FILE_TO_BE_LINKED. It should
00048 /// be used in a .cpp file to define the name referenced in a header file that
00049 /// will cause linkage of the .cpp file. It should only be used at extern level.
00050 #define DEFINING_FILE_FOR(name) \
00051   namespace llvm { const char name ## LinkVar = 0; }
00052 
00053 namespace llvm {
00054 
00055 /// This class is used in the implementation of FORCE_DEFINING_FILE_TO_BE_LINKED
00056 /// macro to make sure that the implementation of a header file is included 
00057 /// into a tool that uses the header.  This is solely 
00058 /// to overcome problems linking .a files and not getting the implementation 
00059 /// of compilation units we need. This is commonly an issue with the various
00060 /// Passes but also occurs elsewhere in LLVM. We like to use .a files because
00061 /// they link faster and provide the smallest executables. However, sometimes
00062 /// those executables are too small, if the program doesn't reference something
00063 /// that might be needed, especially by a loaded share object. This little class
00064 /// helps to resolve that problem. The basic strategy is to use this class in
00065 /// a header file and pass the address of a variable to the constructor. If the
00066 /// variable is defined in the header file's corresponding .cpp file then all
00067 /// tools/libraries that #include the header file will require the .cpp as well.
00068 /// For example:<br/>
00069 /// <tt>extern int LinkMyCodeStub;</tt><br/>
00070 /// <tt>static IncludeFile LinkMyModule(&LinkMyCodeStub);</tt><br/>
00071 /// @brief Class to ensure linking of corresponding object file.
00072 struct IncludeFile {
00073   explicit IncludeFile(const void *);
00074 };
00075 
00076 }
00077 
00078 #endif