MCSymbol.cpp revision 1689e0cf55205042b830fdbe3fc5f7b483997334
1//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCSymbol.h"
11#include "llvm/Support/raw_ostream.h"
12
13using namespace llvm;
14
15/// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it
16/// does not match [a-zA-Z_.][a-zA-Z0-9_.]*.
17//
18// FIXME: This could be more permissive, do we care?
19static inline bool NeedsQuoting(const StringRef &Str) {
20  if (Str.empty())
21    return true;
22
23  // Check that first character is in [a-zA-Z_.].
24  if (!((Str[0] >= 'a' && Str[0] <= 'z') ||
25        (Str[0] >= 'A' && Str[0] <= 'Z') ||
26        (Str[0] == '_' || Str[0] == '.')))
27    return true;
28
29  // Check subsequent characters are in [a-zA-Z0-9_.].
30  for (unsigned i = 1, e = Str.size(); i != e; ++i)
31    if (!((Str[i] >= 'a' && Str[i] <= 'z') ||
32          (Str[i] >= 'A' && Str[i] <= 'Z') ||
33          (Str[i] >= '0' && Str[i] <= '9') ||
34          (Str[i] == '_' || Str[i] == '.')))
35      return true;
36
37  return false;
38}
39
40void MCSymbol::print(raw_ostream &OS) const {
41  if (NeedsQuoting(getName()))
42    OS << '"' << getName() << '"';
43  else
44    OS << getName();
45}
46
47void MCSymbol::dump() const {
48  print(errs());
49}
50