LLVM API Documentation

MCSymbol.cpp

Go to the documentation of this file.
00001 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
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 #include "llvm/MC/MCSymbol.h"
00011 #include "llvm/Support/raw_ostream.h"
00012 
00013 using namespace llvm;
00014 
00015 /// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it
00016 /// does not match [a-zA-Z_.][a-zA-Z0-9_.]*.
00017 //
00018 // FIXME: This could be more permissive, do we care?
00019 static inline bool NeedsQuoting(const StringRef &Str) {
00020   if (Str.empty())
00021     return true;
00022 
00023   // Check that first character is in [a-zA-Z_.].
00024   if (!((Str[0] >= 'a' && Str[0] <= 'z') ||
00025         (Str[0] >= 'A' && Str[0] <= 'Z') ||
00026         (Str[0] == '_' || Str[0] == '.')))
00027     return true;
00028 
00029   // Check subsequent characters are in [a-zA-Z0-9_.].
00030   for (unsigned i = 1, e = Str.size(); i != e; ++i)
00031     if (!((Str[i] >= 'a' && Str[i] <= 'z') ||
00032           (Str[i] >= 'A' && Str[i] <= 'Z') ||
00033           (Str[i] >= '0' && Str[i] <= '9') ||
00034           (Str[i] == '_' || Str[i] == '.')))
00035       return true;
00036 
00037   return false;
00038 }
00039 
00040 void MCSymbol::print(raw_ostream &OS) const {
00041   if (NeedsQuoting(getName()))
00042     OS << '"' << getName() << '"';
00043   else
00044     OS << getName();
00045 }
00046 
00047 void MCSymbol::dump() const {
00048   print(errs());
00049 }