Skip to content

[TableGen] Detect invalid -D arguments and fail #102813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions llvm/lib/TableGen/TGLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ constexpr PreprocessorDir PreprocessorDirs[] = {{tgtok::Ifdef, "ifdef"},
{tgtok::Endif, "endif"},
{tgtok::Define, "define"}};

// Returns a pointer past the end of a valid macro name at the start of `Str`.
// Valid macro names match the regular expression [a-zA-Z_][0-9a-zA-Z_]*.
static const char *lexMacroName(StringRef Str) {
assert(!Str.empty());

// Macro names start with [a-zA-Z_].
const char *Next = Str.begin();
if (*Next != '_' && !isalpha(*Next))
return Next;
// Eat the first character of the name.
++Next;

// Match the rest of the identifier regex: [0-9a-zA-Z_]*
const char *End = Str.end();
while (Next != End && (isalpha(*Next) || isdigit(*Next) || *Next == '_'))
++Next;
return Next;
}

TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
CurBuffer = SrcMgr.getMainFileID();
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
Expand All @@ -54,9 +73,16 @@ TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
PrepIncludeStack.push_back(
std::make_unique<std::vector<PreprocessorControlDesc>>());

// Put all macros defined in the command line into the DefinedMacros set.
for (const std::string &MacroName : Macros)
// Add all macros defined on the command line to the DefinedMacros set.
// Check invalid macro names and print fatal error if we find one.
for (StringRef MacroName : Macros) {
const char *End = lexMacroName(MacroName);
if (End != MacroName.end())
PrintFatalError("Invalid macro name `" + MacroName +
"` specified on command line");

DefinedMacros.insert(MacroName);
}
}

SMLoc TGLexer::getLoc() const {
Expand Down Expand Up @@ -699,9 +725,8 @@ bool TGLexer::prepEatPreprocessorDirective(tgtok::TokKind Kind) {
return false;
}

tgtok::TokKind TGLexer::lexPreprocessor(
tgtok::TokKind Kind, bool ReturnNextLiveToken) {

tgtok::TokKind TGLexer::lexPreprocessor(tgtok::TokKind Kind,
bool ReturnNextLiveToken) {
// We must be looking at a preprocessing directive. Eat it!
if (!prepEatPreprocessorDirective(Kind))
PrintFatalError("lexPreprocessor() called for unknown "
Expand Down Expand Up @@ -901,14 +926,7 @@ StringRef TGLexer::prepLexMacroName() {
++CurPtr;

TokStart = CurPtr;
// Macro names start with [a-zA-Z_].
if (*CurPtr != '_' && !isalpha(*CurPtr))
return "";

// Match the rest of the identifier regex: [0-9a-zA-Z_]*
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
++CurPtr;

CurPtr = lexMacroName(StringRef(CurPtr, CurBuf.end() - CurPtr));
return StringRef(TokStart, CurPtr - TokStart);
}

Expand Down
9 changes: 9 additions & 0 deletions llvm/test/TableGen/invalid-macro-name-command-line.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: not llvm-tblgen %s -DMACRO=1 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-1
// RUN: not llvm-tblgen %s -D0MAC 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-2
// RUN: not llvm-tblgen %s -D_MAC# 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-3
// RUN: not llvm-tblgen %s -D 2>&1 | FileCheck %s --check-prefix=CHECK-TEST-4

// CHECK-TEST-1: error: Invalid macro name `MACRO=1` specified on command line
// CHECK-TEST-2: error: Invalid macro name `0MAC` specified on command line
// CHECK-TEST-3: error: Invalid macro name `_MAC#` specified on command line
// CHECK-TEST-4: for the -D option: requires a value!
Loading