blob: 3b1a41d48d07ae90239ce97964df0919ff4529c6 [file] [log] [blame]
Daniel Dunbara31e6302009-08-14 03:41:23 +00001//===- 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
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +000015// Sentinel value for the absolute pseudo section.
16const MCSection *MCSymbol::AbsolutePseudoSection =
17 reinterpret_cast<const MCSection *>(1);
18
Daniel Dunbara31e6302009-08-14 03:41:23 +000019/// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it
20/// does not match [a-zA-Z_.][a-zA-Z0-9_.]*.
21//
22// FIXME: This could be more permissive, do we care?
23static inline bool NeedsQuoting(const StringRef &Str) {
24 if (Str.empty())
25 return true;
26
27 // Check that first character is in [a-zA-Z_.].
28 if (!((Str[0] >= 'a' && Str[0] <= 'z') ||
29 (Str[0] >= 'A' && Str[0] <= 'Z') ||
30 (Str[0] == '_' || Str[0] == '.')))
31 return true;
32
33 // Check subsequent characters are in [a-zA-Z0-9_.].
34 for (unsigned i = 1, e = Str.size(); i != e; ++i)
35 if (!((Str[i] >= 'a' && Str[i] <= 'z') ||
36 (Str[i] >= 'A' && Str[i] <= 'Z') ||
37 (Str[i] >= '0' && Str[i] <= '9') ||
38 (Str[i] == '_' || Str[i] == '.')))
39 return true;
40
41 return false;
42}
43
Chris Lattner0fe3a1e2009-09-03 05:46:51 +000044void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Daniel Dunbara31e6302009-08-14 03:41:23 +000045 if (NeedsQuoting(getName()))
46 OS << '"' << getName() << '"';
47 else
48 OS << getName();
49}
50
51void MCSymbol::dump() const {
Chris Lattner0fe3a1e2009-09-03 05:46:51 +000052 print(errs(), 0);
Daniel Dunbara31e6302009-08-14 03:41:23 +000053}