Files
revng-revng/lib/Model/Identifier.cpp
Alessandro Di Federico a5b380b201 Model: rework how we name things
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.
2023-08-23 16:14:04 +02:00

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;
}