Intel SPMD Program Compiler  1.3.0
util.h
Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2010-2011, Intel Corporation
00003   All rights reserved.
00004 
00005   Redistribution and use in source and binary forms, with or without
00006   modification, are permitted provided that the following conditions are
00007   met:
00008 
00009     * Redistributions of source code must retain the above copyright
00010       notice, this list of conditions and the following disclaimer.
00011 
00012     * Redistributions in binary form must reproduce the above copyright
00013       notice, this list of conditions and the following disclaimer in the
00014       documentation and/or other materials provided with the distribution.
00015 
00016     * Neither the name of Intel Corporation nor the names of its
00017       contributors may be used to endorse or promote products derived from
00018       this software without specific prior written permission.
00019 
00020 
00021    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00022    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00023    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00024    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00025    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00027    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00032 */
00033 
00034 /** @file util.h
00035 
00036     @brief 
00037 */
00038 
00039 #ifndef ISPC_UTIL_H
00040 #define ISPC_UTIL_H
00041 
00042 #include "ispc.h"
00043 #ifdef ISPC_IS_WINDOWS
00044 #include <stdarg.h>
00045 #endif
00046 
00047 struct SourcePos;
00048 
00049 /** Rounds up the given value to the next power of two, if it isn't a power
00050     of two already. */
00051 inline uint32_t RoundUpPow2(uint32_t v) {
00052     v--;
00053     v |= v >> 1;    
00054     v |= v >> 2;
00055     v |= v >> 4;    
00056     v |= v >> 8;
00057     v |= v >> 16;
00058     return v+1;
00059 }
00060 
00061 #ifdef __GNUG__
00062 #define PRINTF_FUNC __attribute__ \
00063     ((__format__ (__printf__, 2, 3)))
00064 #else
00065 #define PRINTF_FUNC
00066 #endif // __GNUG__
00067 
00068 // for cross-platform compatibility
00069 #ifdef ISPC_IS_WINDOWS
00070 int vasprintf(char **sptr, const char *fmt, va_list argv);
00071 int  asprintf(char **sptr, const char *fmt, ...);
00072 #endif
00073 
00074 /** Prints a debugging message.  These messages are only printed if
00075     g->debugPrint is \c true.  In addition to a program source code
00076     position to associate with the message, a printf()-style format string
00077     is passed along with any values needed for items in the format
00078     string. 
00079 */
00080 void Debug(SourcePos p, const char *format, ...) PRINTF_FUNC;
00081 
00082 /** Prints a warning about an issue found during compilation.  Compilation
00083     can still continue after warnings; they are purely informative for the
00084     user.  In addition to a program source code position to associate with
00085     the message, a printf()-style format string is passed along with any
00086     values needed for items in the format string.
00087 */
00088 void Warning(SourcePos p, const char *format, ...) PRINTF_FUNC;
00089 
00090 /** Prints an error message.  It is assumd that compilation can not be
00091     successfully completed after an error has been issued, though the
00092     system tries to continue compiling as much as possible, in order to be
00093     able to issue any subsequent error messages.  In addition to a program
00094     source code position to associate with the message, a printf()-style
00095     format string is passed along with any values needed for items in the
00096     format string. 
00097 */
00098 void Error(SourcePos p, const char *format, ...) PRINTF_FUNC;
00099 
00100 /** Prints a message about a potential performance issue in the user's
00101     code.  These messages are purely informative and don't affect the
00102     completion of compilation.  In addition to a program source code
00103     position to associate with the message, a printf()-style format string
00104     is passed along with any values needed for items in the format
00105     string.  
00106 */
00107 void PerformanceWarning(SourcePos p, const char *format, ...) PRINTF_FUNC;
00108 
00109 /** Reports a fatal error that causes the program to terminate.  This
00110     should only be used for cases where there is an internal error in the
00111     compiler.
00112  */
00113 #define FATAL(message) FatalError(__FILE__, __LINE__, message)
00114 
00115 /** This function generally shouldn't be called directly, but should be
00116     used via the FATAL macro, which includes the file and line number where
00117     the error was issued.
00118  */
00119 void FatalError(const char *file, int line, const char *message);
00120 
00121 /** Returns the number of single-character edits needed to transform
00122     between the two strings.
00123 
00124     @param str1    First string
00125     @param str2    Second string
00126     @param maxDist Maximum number of single-character edits allowed
00127     @return        Number of single-character edits to transform from str1
00128                    to str2, or maxDist+1 if it's not psosible to do so
00129                    in fewer than maxDist steps
00130 */
00131 int StringEditDistance(const std::string &str1, const std::string &str2,
00132                        int maxDist);
00133 
00134 /** Given a string and a set of candidate strings, returns the set of
00135     candidates that are "close" to the given string, where distance is
00136     measured by the number of single-character changes needed to transform
00137     between the two.  An empty vector may be returned if none of the
00138     options is close to \c str.
00139  */
00140 std::vector<std::string> MatchStrings(const std::string &str,
00141                                       const std::vector<std::string> &options);
00142 
00143 /** Given the current working directory and a filename relative to that
00144     directory, this function returns the final directory that the resulting
00145     file is in and the base name of the file itself. */
00146 void GetDirectoryAndFileName(const std::string &currentDir, 
00147                              const std::string &relativeName,
00148                              std::string *directory, std::string *filename);
00149 
00150 #endif // ISPC_UTIL_H