Bug 1897647 - Generate URL_ analysis item for chrome:// and resource:// string literals in C++ code. r=asuth

Differential Revision: https://phabricator.services.mozilla.com/D210892
This commit is contained in:
Tooru Fujisawa 2024-05-20 14:11:27 +00:00
parent 82ae63cedb
commit 4b6ff158ec

View File

@ -122,6 +122,21 @@ static bool isValidIdentifier(std::string Input) {
return true;
}
template <size_t N>
static bool stringStartsWith(const std::string& Input,
const char (&Prefix)[N]) {
return Input.length() > N - 1 && memcmp(Input.c_str(), Prefix, N - 1) == 0;
}
static bool isASCII(const std::string& Input) {
for (char C : Input) {
if (C & 0x80) {
return false;
}
}
return true;
}
struct RAIITracer {
RAIITracer(const char *log) : mLog(log) {
printf("<%s>\n", mLog);
@ -486,6 +501,10 @@ private:
return Filename;
}
std::string mangleURL(std::string Url) {
return mangleFile(Url, FileType::Source);
}
std::string mangleQualifiedName(std::string Name) {
std::replace(Name.begin(), Name.end(), ' ', '_');
return Name;
@ -2244,6 +2263,35 @@ public:
return true;
}
bool VisitStringLiteral(StringLiteral *E) {
if (E->getCharByteWidth() != 1) {
return true;
}
StringRef sref = E->getString();
std::string s = sref.str();
if (!stringStartsWith(s, "chrome://") &&
!stringStartsWith(s, "resource://")) {
return true;
}
if (!isASCII(s)) {
return true;
}
SourceLocation Loc = E->getStrTokenLoc(0);
normalizeLocation(&Loc);
std::string symbol = std::string("URL_") + mangleURL(s);
visitIdentifier("use", "file", StringRef(s), Loc, symbol,
QualType(), Context(),
NotIdentifierToken | LocRangeEndValid);
return true;
}
void enterSourceFile(SourceLocation Loc) {
normalizeLocation(&Loc);
FileInfo* newFile = getFileInfo(Loc);