From 4b6ff158ec52d60dea201ad7b42538bba8ab30b2 Mon Sep 17 00:00:00 2001 From: Tooru Fujisawa Date: Mon, 20 May 2024 14:11:27 +0000 Subject: [PATCH] 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 --- .../mozsearch-plugin/MozsearchIndexer.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp b/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp index 5e09e093d083..5241d94ec8ae 100644 --- a/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp +++ b/build/clang-plugin/mozsearch-plugin/MozsearchIndexer.cpp @@ -122,6 +122,21 @@ static bool isValidIdentifier(std::string Input) { return true; } +template +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);