Intel SPMD Program Compiler  1.9.1
Functions
util.cpp File Reference

Various small utility routines. More...

#include "util.h"
#include "module.h"
#include <alloca.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <set>
#include <algorithm>
#include <llvm/IR/DataLayout.h>
Include dependency graph for util.cpp:

Go to the source code of this file.

Functions

int TerminalWidth ()
 
static bool lHaveANSIColors ()
 
static const char * lStartBold ()
 
static const char * lStartRed ()
 
static const char * lStartBlue ()
 
static const char * lResetColor ()
 
static const char * lFindWordEnd (const char *buf)
 
static void lPrintFileLineContext (SourcePos p)
 
static int lFindIndent (int numColons, const char *buf)
 
void PrintWithWordBreaks (const char *buf, int indent, int columnWidth, FILE *out)
 
static void lPrint (const char *type, bool isError, SourcePos p, const char *fmt, va_list args)
 
void Error (SourcePos p, const char *fmt,...)
 
void Debug (SourcePos p, const char *fmt,...)
 
void Warning (SourcePos p, const char *fmt,...)
 
void PerformanceWarning (SourcePos p, const char *fmt,...)
 
static void lPrintBugText ()
 
void FatalError (const char *file, int line, const char *message)
 
void DoAssert (const char *file, int line, const char *expr)
 
void DoAssertPos (SourcePos pos, const char *file, int line, const char *expr)
 
int StringEditDistance (const std::string &str1, const std::string &str2, int maxDist)
 
std::vector< std::string > MatchStrings (const std::string &str, const std::vector< std::string > &options)
 
void GetDirectoryAndFileName (const std::string &currentDirectory, const std::string &relativeName, std::string *directory, std::string *filename)
 
static std::set< std::string > lGetStringArray (const std::string &str)
 
bool VerifyDataLayoutCompatibility (const std::string &module_dl, const std::string &lib_dl)
 

Detailed Description

Various small utility routines.

Definition in file util.cpp.

Function Documentation

void Debug ( SourcePos  p,
const char *  format,
  ... 
)

Prints a debugging message. These messages are only printed if g->debugPrint is true. In addition to a program source code position to associate with the message, a printf()-style format string is passed along with any values needed for items in the format string.

Definition at line 398 of file util.cpp.

References Globals::debugPrint, g, lPrint(), and Globals::quiet.

Referenced by AddBitcodeToModule(), Function::emitCode(), IfStmt::emitVaryingIf(), ConstExpr::GetConstant(), SelectExpr::GetValue(), lApplyLoad1(), lApplyLoad2(), lApplyLoad4(), lAssemble4Vector(), lCreateISPCSymbol(), lCreateSymbol(), lEmitLoads(), lGSToLoadStore(), LLVMVectorIsLinear(), LLVMVectorValuesAllEqual(), lSelectLoads(), lStripUnusedDebugInfo(), BinaryExpr::Optimize(), GatherCoalescePass::runOnBasicBlock(), and TypeConvertExpr().

void DoAssert ( const char *  file,
int  line,
const char *  expr 
)

Definition at line 463 of file util.cpp.

References lPrintBugText().

void DoAssertPos ( SourcePos  pos,
const char *  file,
int  line,
const char *  expr 
)

Definition at line 471 of file util.cpp.

References Error(), and lPrintBugText().

void Error ( SourcePos  p,
const char *  format,
  ... 
)

Prints an error message. It is assumd that compilation can not be successfully completed after an error has been issued, though the system tries to continue compiling as much as possible, in order to be able to issue any subsequent error messages. In addition to a program source code position to associate with the message, a printf()-style format string is passed along with any values needed for items in the format string.

Definition at line 385 of file util.cpp.

References Module::errorCount, g, lPrint(), m, and Globals::quiet.

Referenced by AddBitcodeToModule(), Module::AddExportedTypes(), Module::AddFunctionDeclaration(), Module::AddGlobalVariable(), SymbolTable::AddType(), SymbolTable::AddVariable(), FunctionEmitContext::Break(), StructType::checkIfCanBeSOA(), Module::CompileAndOutput(), FunctionEmitContext::Continue(), MemberExpr::create(), FunctionEmitContext::CurrentLanesReturned(), DoAssertPos(), FunctionEmitContext::EmitCaseLabel(), DeclStmt::EmitCode(), ReturnStmt::EmitCode(), GotoStmt::EmitCode(), AssertStmt::EmitCode(), FunctionEmitContext::EmitDefaultLabel(), emitOffloadParamStruct(), Function::GenerateIR(), DeclSpecs::GetBaseType(), ExprList::GetConstant(), FunctionSymbolExpr::GetConstant(), StructMemberExpr::getElementNumber(), VectorMemberExpr::getElementNumber(), GetStructTypesNamesPositions(), FunctionSymbolExpr::GetType(), StructMemberExpr::GetType(), AssignExpr::GetValue(), FunctionCallExpr::GetValue(), VectorMemberExpr::GetValue(), Declaration::GetVariableDeclarations(), Declarator::InitFromDeclSpecs(), Declarator::InitFromType(), FunctionEmitContext::initLabelBBlocks(), InitSymbol(), lApplyTypeQualifiers(), lCheckExportedParameterTypes(), lCheckForConstStructMember(), lCheckForStructParameters(), lConstFoldBinaryArithOp(), lCreateBinaryOperatorCall(), lCreateDispatchFunction(), lDoTypeConv(), lEmitOpAssign(), lEmitPrePostIncDec(), lGetSystemISA(), FunctionEmitContext::LoadInst(), lProcessPrintArg(), lSwitchASTPreVisit(), lVaryingStructHasUniformMember(), lVectorConvert(), Type::MoreGeneralType(), NewExpr::NewExpr(), FunctionSymbolExpr::ResolveOverloads(), ArrayType::SizeUnsizedArrays(), FunctionEmitContext::StartForeach(), FunctionEmitContext::StoreInst(), StructType::StructType(), Target::Target(), UnaryExpr::TypeCheck(), BinaryExpr::TypeCheck(), AssignExpr::TypeCheck(), ForeachStmt::TypeCheck(), FunctionCallExpr::TypeCheck(), IndexExpr::TypeCheck(), ForeachUniqueStmt::TypeCheck(), LabeledStmt::TypeCheck(), TypeCastExpr::TypeCheck(), PtrDerefExpr::TypeCheck(), DeleteStmt::TypeCheck(), AddressOfExpr::TypeCheck(), SizeOfExpr::TypeCheck(), NewExpr::TypeCheck(), Module::writeDevStub(), and Module::writeOutput().

void FatalError ( const char *  file,
int  line,
const char *  message 
)

This function generally shouldn't be called directly, but should be used via the FATAL macro, which includes the file and line number where the error was issued.

Definition at line 455 of file util.cpp.

References lPrintBugText().

void GetDirectoryAndFileName ( const std::string &  currentDir,
const std::string &  relativeName,
std::string *  directory,
std::string *  filename 
)

Given the current working directory and a filename relative to that directory, this function returns the final directory that the resulting file is in and the base name of the file itself.

Definition at line 547 of file util.cpp.

References Assert, Globals::currentDirectory, and g.

Referenced by SourcePos::GetDIFile(), and Module::Module().

static int lFindIndent ( int  numColons,
const char *  buf 
)
static

Counts the number of characters into the buf at which the numColons colon character is found. Skips over ANSI escape sequences and doesn't include their characters in the final count.

Definition at line 204 of file util.cpp.

Referenced by lPrint().

static const char* lFindWordEnd ( const char *  buf)
static

Given a pointer into a string, find the end of the current word and return a pointer to its last character.

Definition at line 152 of file util.cpp.

Referenced by PrintWithWordBreaks().

static std::set<std::string> lGetStringArray ( const std::string &  str)
static

Definition at line 584 of file util.cpp.

References Assert.

Referenced by VerifyDataLayoutCompatibility().

static bool lHaveANSIColors ( )
static

Definition at line 102 of file util.cpp.

References Globals::forceColoredOutput, and g.

Referenced by lResetColor(), lStartBlue(), lStartBold(), and lStartRed().

static void lPrint ( const char *  type,
bool  isError,
SourcePos  p,
const char *  fmt,
va_list  args 
)
static

Helper function for Error(), Warning(), etc.

Parameters
typeThe type of message being printed (e.g. "Warning")
pPosition in source file that is connected to the message being printed
fmtprintf()-style format string
argsArguments with values for format string % entries

Definition at line 333 of file util.cpp.

References SourcePos::first_column, SourcePos::first_line, lFindIndent(), lPrintFileLineContext(), lResetColor(), lStartBlue(), lStartBold(), lStartRed(), SourcePos::name, PrintWithWordBreaks(), and TerminalWidth().

Referenced by Debug(), Error(), PerformanceWarning(), and Warning().

static void lPrintBugText ( )
static

Definition at line 439 of file util.cpp.

Referenced by DoAssert(), DoAssertPos(), and FatalError().

static void lPrintFileLineContext ( SourcePos  p)
static

When printing error messages, we sometimes want to include the source file line for context. This function print the line(s) of the file corresponding to the provided SourcePos and underlines the range of the SourcePos with '^' symbols.

Definition at line 164 of file util.cpp.

References SourcePos::first_column, SourcePos::first_line, SourcePos::last_column, SourcePos::last_line, and SourcePos::name.

Referenced by lPrint().

static const char* lResetColor ( )
static

Definition at line 141 of file util.cpp.

References lHaveANSIColors().

Referenced by lPrint().

static const char* lStartBlue ( )
static

Definition at line 132 of file util.cpp.

References lHaveANSIColors().

Referenced by lPrint().

static const char* lStartBold ( )
static

Definition at line 114 of file util.cpp.

References lHaveANSIColors().

Referenced by lPrint().

static const char* lStartRed ( )
static

Definition at line 123 of file util.cpp.

References lHaveANSIColors().

Referenced by lPrint().

std::vector<std::string> MatchStrings ( const std::string &  str,
const std::vector< std::string > &  options 
)

Given a string and a set of candidate strings, returns the set of candidates that are "close" to the given string, where distance is measured by the number of single-character changes needed to transform between the two. An empty vector may be returned if none of the options is close to str.

Definition at line 519 of file util.cpp.

References StringEditDistance().

Referenced by GotoStmt::EmitCode(), and MemberExpr::getCandidateNearMatches().

void PerformanceWarning ( SourcePos  p,
const char *  format,
  ... 
)

Prints a message about a potential performance issue in the user's code. These messages are purely informative and don't affect the completion of compilation. In addition to a program source code position to associate with the message, a printf()-style format string is passed along with any values needed for items in the format string.

Definition at line 426 of file util.cpp.

References Globals::emitPerfWarnings, g, lPrint(), SourcePos::name, and Globals::quiet.

Referenced by DeclStmt::EmitCode(), DeclSpecs::GetBaseType(), BinaryExpr::GetValue(), lCoalescePerfInfo(), lEmitBinaryArith(), lReplacePseudoGS(), lTypeConvAtomic(), and ReplaceStdlibShiftPass::runOnBasicBlock().

void PrintWithWordBreaks ( const char *  buf,
int  indent,
int  columnWidth,
FILE *  out 
)

Print the given string to the given FILE, assuming the given output column width. Break words as needed to avoid words spilling past the last column.

Definition at line 230 of file util.cpp.

References lFindWordEnd().

Referenced by lPrint(), and usage().

int StringEditDistance ( const std::string &  str1,
const std::string &  str2,
int  maxDist 
)

Returns the number of single-character edits needed to transform between the two strings.

Parameters
str1First string
str2Second string
maxDistMaximum number of single-character edits allowed
Returns
Number of single-character edits to transform from str1 to str2, or maxDist+1 if it's not psosible to do so in fewer than maxDist steps

Definition at line 482 of file util.cpp.

Referenced by SymbolTable::closestTypeMatch(), SymbolTable::ClosestVariableOrFunctionMatch(), and MatchStrings().

int TerminalWidth ( )

Returns the width of the terminal where the compiler is running. Finding this out may fail in a variety of reasonable situations (piping compiler output to 'less', redirecting output to a file, running the compiler under a debuffer; in this case, just return a reasonable default.

Definition at line 81 of file util.cpp.

References Globals::disableLineWrap, and g.

Referenced by lPrint(), and usage().

bool VerifyDataLayoutCompatibility ( const std::string &  module_dl,
const std::string &  lib_dl 
)

Verification routine, which ensures that DataLayout of the module being compiled is compatible with DataLayout of the library. At the moment we allow the library DataLayout to a subset of the module DataLayout (and extra floating point and vector types to be defined for module) or empty library DataLayout.

Definition at line 603 of file util.cpp.

References lGetStringArray().

Referenced by AddBitcodeToModule().

void Warning ( SourcePos  p,
const char *  format,
  ... 
)

Prints a warning about an issue found during compilation. Compilation can still continue after warnings; they are purely informative for the user. In addition to a program source code position to associate with the message, a printf()-style format string is passed along with any values needed for items in the format string.

Definition at line 410 of file util.cpp.

References Globals::disableWarnings, Module::errorCount, g, lPrint(), m, Globals::quiet, and Globals::warningsAsErrors.

Referenced by AddBitcodeToModule(), Module::AddFunctionDeclaration(), SymbolTable::AddVariable(), Function::emitCode(), IfStmt::EmitCode(), DoStmt::EmitCode(), ForStmt::EmitCode(), DeclSpecs::GetBaseType(), TypeCastExpr::GetValue(), Declarator::InitFromType(), lCheckIndicesVersusBounds(), lExtractOrCheckGlobals(), lGSToLoadStore(), FunctionEmitContext::LoadInst(), main(), BinaryExpr::Optimize(), FunctionSymbolExpr::ResolveOverloads(), Target::Target(), TypeCastExpr::TypeCheck(), Module::writeDevStub(), Module::writeHostStub(), and Module::writeOutput().