Skip to content

Commit bff288e

Browse files
committed
[NFC][TableGen] Refactor SequenceToOffsetTable class.
- Replace use of std::isalnum/ispunct with StringExtras version to avoid possibly locale dependent behavior. - Add `isPunct` to StringExtras. - Remove `static` from printChar (do its deduplicated when linking). - Use range based for loops and structured bindings. - No need to use `llvm::` for code in llvm namespace.
1 parent eb7d535 commit bff288e

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

llvm/include/llvm/ADT/StringExtras.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ inline bool isPrint(char C) {
140140
return (0x20 <= UC) && (UC <= 0x7E);
141141
}
142142

143+
/// Checks whether character \p C is a punctuation character.
144+
///
145+
/// Locale-independent version of the C standard library ispunct.
146+
inline bool isPunct(char C) {
147+
static constexpr StringRef Punctuations =
148+
R"((!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~))";
149+
return Punctuations.contains(C);
150+
}
151+
143152
/// Checks whether character \p C is whitespace in the "C" locale.
144153
///
145154
/// Locale-independent version of the C standard library isspace.

llvm/utils/TableGen/Basic/SequenceToOffsetTable.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
#ifndef LLVM_UTILS_TABLEGEN_BASIC_SEQUENCETOOFFSETTABLE_H
1616
#define LLVM_UTILS_TABLEGEN_BASIC_SEQUENCETOOFFSETTABLE_H
1717

18+
#include "llvm/ADT/StringExtras.h"
1819
#include "llvm/Support/CommandLine.h"
1920
#include "llvm/Support/raw_ostream.h"
2021
#include <algorithm>
2122
#include <cassert>
22-
#include <cctype>
2323
#include <functional>
2424
#include <map>
2525

2626
namespace llvm {
27-
extern llvm::cl::opt<bool> EmitLongStrLiterals;
27+
extern cl::opt<bool> EmitLongStrLiterals;
2828

29-
static inline void printChar(raw_ostream &OS, char C) {
29+
inline void printChar(raw_ostream &OS, char C) {
3030
unsigned char UC(C);
31-
if (isalnum(UC) || ispunct(UC)) {
31+
if (isAlnum(UC) || isPunct(UC)) {
3232
OS << '\'';
3333
if (C == '\\' || C == '\'')
3434
OS << '\\';
@@ -126,7 +126,7 @@ class SequenceToOffsetTable {
126126
/// initializer, where each element is a C string literal terminated by
127127
/// `\0`. Falls back to emitting a comma-separated integer list if
128128
/// `EmitLongStrLiterals` is false
129-
void emitStringLiteralDef(raw_ostream &OS, const llvm::Twine &Decl) const {
129+
void emitStringLiteralDef(raw_ostream &OS, const Twine &Decl) const {
130130
assert(Entries && "Call layout() before emitStringLiteralDef()");
131131
if (!EmitLongStrLiterals) {
132132
OS << Decl << " = {\n";
@@ -140,9 +140,9 @@ class SequenceToOffsetTable {
140140
<< "#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n"
141141
<< "#endif\n"
142142
<< Decl << " = {\n";
143-
for (auto I : Seqs) {
144-
OS << " /* " << I.second << " */ \"";
145-
OS.write_escaped(I.first);
143+
for (const auto &[Seq, Offset] : Seqs) {
144+
OS << " /* " << Offset << " */ \"";
145+
OS.write_escaped(Seq);
146146
OS << "\\0\"\n";
147147
}
148148
OS << "};\n"
@@ -156,13 +156,10 @@ class SequenceToOffsetTable {
156156
void emit(raw_ostream &OS, void (*Print)(raw_ostream &, ElemT),
157157
const char *Term = "0") const {
158158
assert((empty() || Entries) && "Call layout() before emit()");
159-
for (typename SeqMap::const_iterator I = Seqs.begin(), E = Seqs.end();
160-
I != E; ++I) {
161-
OS << " /* " << I->second << " */ ";
162-
for (typename SeqT::const_iterator SI = I->first.begin(),
163-
SE = I->first.end();
164-
SI != SE; ++SI) {
165-
Print(OS, *SI);
159+
for (const auto &[Seq, Offset] : Seqs) {
160+
OS << " /* " << Offset << " */ ";
161+
for (const ElemT &Element : Seq) {
162+
Print(OS, Element);
166163
OS << ", ";
167164
}
168165
OS << Term << ",\n";

0 commit comments

Comments
 (0)