[MC] Common symbols weren't being checked for redeclaration which allowed an assembly...
[oota-llvm.git] / lib / MC / MCSymbol.cpp
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/MC/MCExpr.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 // Sentinel value for the absolute pseudo section.
17 MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
18
19 static bool isAcceptableChar(char C) {
20   if ((C < 'a' || C > 'z') &&
21       (C < 'A' || C > 'Z') &&
22       (C < '0' || C > '9') &&
23       C != '_' && C != '$' && C != '.' && C != '@')
24     return false;
25   return true;
26 }
27
28 /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
29 /// syntactically correct.
30 static bool NameNeedsQuoting(StringRef Str) {
31   assert(!Str.empty() && "Cannot create an empty MCSymbol");
32
33   // If any of the characters in the string is an unacceptable character, force
34   // quotes.
35   for (unsigned i = 0, e = Str.size(); i != e; ++i)
36     if (!isAcceptableChar(Str[i]))
37       return true;
38   return false;
39 }
40
41 void MCSymbol::setVariableValue(const MCExpr *Value) {
42   assert(!IsUsed && "Cannot set a variable that has already been used.");
43   assert(Value && "Invalid variable value!");
44   this->Value = Value;
45   Section = nullptr;
46   HasFragment = false;
47 }
48
49 void MCSymbol::print(raw_ostream &OS) const {
50   // The name for this MCSymbol is required to be a valid target name.  However,
51   // some targets support quoting names with funny characters.  If the name
52   // contains a funny character, then print it quoted.
53   StringRef Name = getName();
54   if (Name.empty()) {
55     OS << "\"\"";
56     return;
57   }
58   if (!NameNeedsQuoting(Name)) {
59     OS << Name;
60     return;
61   }
62
63   OS << '"';
64   for (unsigned I = 0, E = Name.size(); I != E; ++I) {
65     char C = Name[I];
66     if (C == '\n')
67       OS << "\\n";
68     else if (C == '"')
69       OS << "\\\"";
70     else
71       OS << C;
72   }
73   OS << '"';
74 }
75
76 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
77 void MCSymbol::dump() const { dbgs() << *this; }
78 #endif