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 was developed by Reid Spencer and is distributed under the 
00006 // University of Illinois Open Source 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 #define FORCE_DEFINING_FILE_TO_BE_LINKED(name) \
00030   namespace llvm { \
00031     extern char name ## LinkVar; \
00032     static IncludeFile name ## LinkObj ( &name ## LinkVar ); \
00033   } 
00034 
00035 /// This macro is the counterpart to FORCE_DEFINING_FILE_TO_BE_LINKED. It should
00036 /// be used in a .cpp file to define the name referenced in a header file that
00037 /// will cause linkage of the .cpp file. It should only be used at extern level.
00038 #define DEFINING_FILE_FOR(name) namespace llvm { char name ## LinkVar; }
00039 
00040 namespace llvm {
00041 
00042 /// This class is used in the implementation of FORCE_DEFINING_FILE_TO_BE_LINKED
00043 /// macro to make sure that the implementation of a header file is included 
00044 /// into a tool that uses the header.  This is solely 
00045 /// to overcome problems linking .a files and not getting the implementation 
00046 /// of compilation units we need. This is commonly an issue with the various
00047 /// Passes but also occurs elsewhere in LLVM. We like to use .a files because
00048 /// they link faster and provide the smallest executables. However, sometimes
00049 /// those executables are too small, if the program doesn't reference something
00050 /// that might be needed, especially by a loaded share object. This little class
00051 /// helps to resolve that problem. The basic strategy is to use this class in
00052 /// a header file and pass the address of a variable to the constructor. If the
00053 /// variable is defined in the header file's corresponding .cpp file then all
00054 /// tools/libraries that #include the header file will require the .cpp as well.
00055 /// For example:<br/>
00056 /// <tt>extern int LinkMyCodeStub;</tt><br/>
00057 /// <tt>static IncludeFile LinkMyModule(&LinkMyCodeStub);</tt><br/>
00058 /// @brief Class to ensure linking of corresponding object file.
00059 struct IncludeFile {
00060   IncludeFile(void *);
00061 };
00062 
00063 }
00064 
00065 #endif