Skip to content

Commit 7aab16a

Browse files
committed
[TableGen] Detect invalid -D arguments and fail.
- Detect invalid macro names specified on command line and fail if one found. - Specifically, -DXYZ=1 for example, will fail instead is being silently accepted.
1 parent b8fc97f commit 7aab16a

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

llvm/lib/TableGen/TGLexer.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ constexpr PreprocessorDir PreprocessorDirs[] = {{tgtok::Ifdef, "ifdef"},
4444
{tgtok::Endif, "endif"},
4545
{tgtok::Define, "define"}};
4646

47+
// Returns a pointer past the end of a valid macro name at the start of `Str`.
48+
// Valid macro names match the regular expression [a-zA-Z_][0-9a-zA-Z_]*.
49+
static const char *lexMacroName(StringRef Str) {
50+
assert(!Str.empty());
51+
52+
const char *Next = Str.begin();
53+
54+
// Macro names start with [a-zA-Z_].
55+
if (*Next != '_' && !isalpha(*Next))
56+
return Next;
57+
58+
// Eat the first character of the name.
59+
Next++;
60+
61+
const char *End = Str.end();
62+
// Match the rest of the identifier regex: [0-9a-zA-Z_]*
63+
while (Next != End && (isalpha(*Next) || isdigit(*Next) || *Next == '_'))
64+
++Next;
65+
66+
return Next;
67+
}
68+
4769
TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
4870
CurBuffer = SrcMgr.getMainFileID();
4971
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
@@ -54,9 +76,16 @@ TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
5476
PrepIncludeStack.push_back(
5577
std::make_unique<std::vector<PreprocessorControlDesc>>());
5678

57-
// Put all macros defined in the command line into the DefinedMacros set.
58-
for (const std::string &MacroName : Macros)
79+
// Add all macros defined on the command line to the DefinedMacros set.
80+
// Check invalid macro names and print fatal error if we find one.
81+
for (StringRef MacroName : Macros) {
82+
const char *End = lexMacroName(MacroName);
83+
if (End != MacroName.end())
84+
PrintFatalError("Invalid macro name `" + MacroName +
85+
"` specified on command line");
86+
5987
DefinedMacros.insert(MacroName);
88+
}
6089
}
6190

6291
SMLoc TGLexer::getLoc() const {
@@ -699,9 +728,8 @@ bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
699728
return false;
700729
}
701730

702-
tgtok::TokKind TGLexer::lexPreprocessor(
703-
tgtok::TokKind Kind, bool ReturnNextLiveToken) {
704-
731+
tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
732+
bool ReturnNextLiveToken) {
705733
// We must be looking at a preprocessing directive. Eat it!
706734
if (!prepEatPreprocessorDirective(Kind))
707735
PrintFatalError("lexPreprocessor() called for unknown "
@@ -901,14 +929,7 @@ StringRef TGLexer::prepLexMacroName() {
901929
++CurPtr;
902930

903931
TokStart = CurPtr;
904-
// Macro names start with [a-zA-Z_].
905-
if (*CurPtr != '_' && !isalpha(*CurPtr))
906-
return "";
907-
908-
// Match the rest of the identifier regex: [0-9a-zA-Z_]*
909-
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
910-
++CurPtr;
911-
932+
CurPtr = lexMacroName(StringRef(CurPtr, CurBuf.end() - CurPtr));
912933
return StringRef(TokStart, CurPtr - TokStart);
913934
}
914935

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not llvm-tblgen %s -DMACRO=1 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-1
2+
// RUN: not llvm-tblgen %s -D0MAC 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-2
3+
// RUN: not llvm-tblgen %s -D_MAC# 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-3
4+
// RUN: not llvm-tblgen %s -D 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-4
5+
6+
// CHECK-TEST-1: error: Invalid macro name `MACRO=1` specified on command line
7+
// CHECK-TEST-2: error: Invalid macro name `0MAC` specified on command line
8+
// CHECK-TEST-3: error: Invalid macro name `_MAC#` specified on command line
9+
// CHECK-TEST-4: for the -D option: requires a value!

0 commit comments

Comments
 (0)