mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
a5b380b201
This commit improves the formalization of how we handle names. The main changes are: * Now `_` is a reserved prefix and all the generated names start with `_`. * The model verification routine now checks that `CustomName`s in the global scope do not collide with any local namespace (e.g., fields of a `StructType`). * We changed the prefix `prefix_` to `unreserved_` to better convey the fact that the prefix has been introduce to use an non-reserved name.
47 lines
1.0 KiB
C++
47 lines
1.0 KiB
C++
/// \file Identifier.cpp
|
|
|
|
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
#include "revng/Model/Identifier.h"
|
|
|
|
using namespace model;
|
|
|
|
Identifier Identifier::fromString(llvm::StringRef Name) {
|
|
revng_assert(not Name.empty());
|
|
Identifier Result;
|
|
|
|
// For reserved C keywords prepend a non-reserved prefix and we're done.
|
|
if (ReservedKeywords.contains(Name)) {
|
|
Result += PrefixForReservedNames;
|
|
Result += Name;
|
|
return Result;
|
|
}
|
|
|
|
const auto BecomesUnderscore = [](const char C) {
|
|
return not std::isalnum(C) or C == '_';
|
|
};
|
|
|
|
// For invalid C identifiers prepend the our reserved prefix.
|
|
if (std::isdigit(Name[0]) or BecomesUnderscore(Name[0])) {
|
|
Result += PrefixForReservedNames;
|
|
}
|
|
|
|
// Append the rest of the name
|
|
Result += Name;
|
|
|
|
return sanitize(Result);
|
|
}
|
|
|
|
Identifier Identifier::sanitize(llvm::StringRef Name) {
|
|
Identifier Result(Name);
|
|
|
|
// Convert all non-alphanumeric chars to underscores
|
|
for (char &C : Result)
|
|
if (not std::isalnum(C))
|
|
C = '_';
|
|
|
|
return Result;
|
|
}
|