Skip to content

Commit

Permalink
Merge branch 'llvm:main' into llvmgh-101657
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-sh authored Jan 17, 2025
2 parents 021ca06 + 89305c3 commit 49cc9bb
Show file tree
Hide file tree
Showing 197 changed files with 9,964 additions and 4,923 deletions.
95 changes: 64 additions & 31 deletions clang-tools-extra/clang-tidy/modernize/RawStringLiteralCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#include "RawStringLiteralCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/StringRef.h"
#include <optional>

using namespace clang::ast_matchers;

Expand Down Expand Up @@ -66,20 +70,6 @@ bool containsDelimiter(StringRef Bytes, const std::string &Delimiter) {
: (")" + Delimiter + R"(")")) != StringRef::npos;
}

std::string asRawStringLiteral(const StringLiteral *Literal,
const std::string &DelimiterStem) {
const StringRef Bytes = Literal->getBytes();
std::string Delimiter;
for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
}

if (Delimiter.empty())
return (R"(R"()" + Bytes + R"lit()")lit").str();

return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")").str();
}

} // namespace

RawStringLiteralCheck::RawStringLiteralCheck(StringRef Name,
Expand Down Expand Up @@ -119,30 +109,73 @@ void RawStringLiteralCheck::registerMatchers(MatchFinder *Finder) {
stringLiteral(unless(hasParent(predefinedExpr()))).bind("lit"), this);
}

static std::optional<StringRef>
createUserDefinedSuffix(const StringLiteral *Literal, const SourceManager &SM,
const LangOptions &LangOpts) {
const CharSourceRange TokenRange =
CharSourceRange::getTokenRange(Literal->getSourceRange());
Token T;
if (Lexer::getRawToken(Literal->getBeginLoc(), T, SM, LangOpts))
return std::nullopt;
const CharSourceRange CharRange =
Lexer::makeFileCharRange(TokenRange, SM, LangOpts);
if (T.hasUDSuffix()) {
StringRef Text = Lexer::getSourceText(CharRange, SM, LangOpts);
const size_t UDSuffixPos = Text.find_last_of('"');
if (UDSuffixPos == StringRef::npos)
return std::nullopt;
return Text.slice(UDSuffixPos + 1, Text.size());
}
return std::nullopt;
}

static std::string createRawStringLiteral(const StringLiteral *Literal,
const std::string &DelimiterStem,
const SourceManager &SM,
const LangOptions &LangOpts) {
const StringRef Bytes = Literal->getBytes();
std::string Delimiter;
for (int I = 0; containsDelimiter(Bytes, Delimiter); ++I) {
Delimiter = (I == 0) ? DelimiterStem : DelimiterStem + std::to_string(I);
}

std::optional<StringRef> UserDefinedSuffix =
createUserDefinedSuffix(Literal, SM, LangOpts);

if (Delimiter.empty())
return (R"(R"()" + Bytes + R"lit()")lit" + UserDefinedSuffix.value_or(""))
.str();

return (R"(R")" + Delimiter + "(" + Bytes + ")" + Delimiter + R"(")" +
UserDefinedSuffix.value_or(""))
.str();
}

static bool compareStringLength(StringRef Replacement,
const StringLiteral *Literal,
const SourceManager &SM,
const LangOptions &LangOpts) {
return Replacement.size() <=
Lexer::MeasureTokenLength(Literal->getBeginLoc(), SM, LangOpts);
}

void RawStringLiteralCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("lit");
if (Literal->getBeginLoc().isMacroID())
return;

const SourceManager &SM = *Result.SourceManager;
const LangOptions &LangOpts = getLangOpts();
if (containsEscapedCharacters(Result, Literal, DisallowedChars)) {
std::string Replacement = asRawStringLiteral(Literal, DelimiterStem);
const std::string Replacement =
createRawStringLiteral(Literal, DelimiterStem, SM, LangOpts);
if (ReplaceShorterLiterals ||
Replacement.length() <=
Lexer::MeasureTokenLength(Literal->getBeginLoc(),
*Result.SourceManager, getLangOpts()))
replaceWithRawStringLiteral(Result, Literal, Replacement);
compareStringLength(Replacement, Literal, SM, LangOpts)) {
diag(Literal->getBeginLoc(),
"escaped string literal can be written as a raw string literal")
<< FixItHint::CreateReplacement(Literal->getSourceRange(),
Replacement);
}
}
}

void RawStringLiteralCheck::replaceWithRawStringLiteral(
const MatchFinder::MatchResult &Result, const StringLiteral *Literal,
StringRef Replacement) {
CharSourceRange CharRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Literal->getSourceRange()),
*Result.SourceManager, getLangOpts());
diag(Literal->getBeginLoc(),
"escaped string literal can be written as a raw string literal")
<< FixItHint::CreateReplacement(CharRange, Replacement);
}

} // namespace clang::tidy::modernize
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class RawStringLiteralCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;

private:
void replaceWithRawStringLiteral(
const ast_matchers::MatchFinder::MatchResult &Result,
const StringLiteral *Literal, StringRef Replacement);

std::string DelimiterStem;
CharsBitSet DisallowedChars;
const bool ReplaceShorterLiterals;
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ Changes in existing checks
a false positive when only an implicit conversion happened inside an
initializer list.

- Improved :doc:`modernize-raw-string-literal
<clang-tidy/checks/modernize/raw-string-literal>` check to fix incorrect
fix-it when the string contains a user-defined suffix.

- Improved :doc:`modernize-use-designated-initializers
<clang-tidy/checks/modernize/use-designated-initializers>` check to fix a
crash when a class is declared but not defined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,16 @@ void callFn() {
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}} can be written as a raw string literal
// CHECK-FIXES: {{^}} fn<double>(R"(foo\bar)");{{$}}
}

namespace std {
using size_t = decltype(sizeof(0));
namespace ud {
int operator""_abc(const char *str, std::size_t len);
} // namespace ud
} // namespace std
namespace gh97243 {
using namespace std::ud;
auto UserDefinedLiteral = "foo\\bar"_abc;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: {{.*}} can be written as a raw string literal
// CHECK-FIXES: {{^}}auto UserDefinedLiteral = R"(foo\bar)"_abc;
} // namespace gh97243
2 changes: 1 addition & 1 deletion clang/cmake/caches/Fuchsia-stage2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ if(FUCHSIA_SDK)
set(LLVM_RUNTIME_MULTILIB_hwasan+noexcept_TARGETS "aarch64-unknown-fuchsia;riscv64-unknown-fuchsia" CACHE STRING "")
endif()

foreach(target armv6m-none-eabi;armv7m-none-eabi;armv8m.main-none-eabi;armv8.1m.main-none-eabi;aarch64-none-elf)
foreach(target armv6m-none-eabi;armv7m-none-eabi;armv7em-none-eabi;armv8m.main-none-eabi;armv8.1m.main-none-eabi;aarch64-none-elf)
list(APPEND BUILTIN_TARGETS "${target}")
set(BUILTINS_${target}_CMAKE_SYSTEM_NAME Generic CACHE STRING "")
set(BUILTINS_${target}_CMAKE_SYSTEM_PROCESSOR arm CACHE STRING "")
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,13 @@ class ASTContext : public RefCountedBase<ASTContext> {

QualType getEnumType(const EnumDecl *Decl) const;

/// Compute BestType and BestPromotionType for an enum based on the highest
/// number of negative and positive bits of its elements.
/// Returns true if enum width is too large.
bool computeBestEnumTypes(bool IsPacked, unsigned NumNegativeBits,
unsigned NumPositiveBits, QualType &BestType,
QualType &BestPromotionType);

QualType
getUnresolvedUsingType(const UnresolvedUsingTypenameDecl *Decl) const;

Expand Down
11 changes: 7 additions & 4 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8841,13 +8841,16 @@ void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val,
unsigned Scale);

inline FunctionEffectsRef FunctionEffectsRef::get(QualType QT) {
const Type *TypePtr = QT.getTypePtr();
while (true) {
QualType Pointee = QT->getPointeeType();
if (Pointee.isNull())
if (QualType Pointee = TypePtr->getPointeeType(); !Pointee.isNull())
TypePtr = Pointee.getTypePtr();
else if (TypePtr->isArrayType())
TypePtr = TypePtr->getBaseElementTypeUnsafe();
else
break;
QT = Pointee;
}
if (const auto *FPT = QT->getAs<FunctionProtoType>())
if (const auto *FPT = TypePtr->getAs<FunctionProtoType>())
return FPT->getFunctionEffects();
return {};
}
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ class ConcreteTypeLoc : public Base {
unsigned extraAlign = asDerived()->getExtraLocalDataAlignment();
size = llvm::alignTo(size, extraAlign);
size += asDerived()->getExtraLocalDataSize();
size = llvm::alignTo(size, asDerived()->getLocalDataAlignment());
return size;
}

Expand Down
8 changes: 4 additions & 4 deletions clang/include/clang/Basic/BuiltinsX86_64.td
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ let Features = "amx-complex,amx-transpose", Attributes = [NoThrow] in {

let Features = "amx-avx512,avx10.2-512", Attributes = [NoThrow] in {
def tcvtrowd2ps_internal : X86Builtin<"_Vector<16, float>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tcvtrowps2pbf16h_internal : X86Builtin<"_Vector<32, __bf16>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tcvtrowps2pbf16l_internal : X86Builtin<"_Vector<32, __bf16>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tcvtrowps2bf16h_internal : X86Builtin<"_Vector<32, __bf16>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tcvtrowps2bf16l_internal : X86Builtin<"_Vector<32, __bf16>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tcvtrowps2phh_internal : X86Builtin<"_Vector<32, _Float16>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tcvtrowps2phl_internal : X86Builtin<"_Vector<32, _Float16>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
def tilemovrow_internal : X86Builtin<"_Vector<16, int>(unsigned short, unsigned short, _Vector<256, int>, unsigned int)">;
Expand Down Expand Up @@ -387,8 +387,8 @@ let Features = "amx-complex,amx-transpose", Attributes = [NoThrow] in {

let Features = "amx-avx512,avx10.2-512", Attributes = [NoThrow] in {
def tcvtrowd2ps : X86Builtin<"_Vector<16, float>(_Constant unsigned char, unsigned int)">;
def tcvtrowps2pbf16h : X86Builtin<"_Vector<32, __bf16>(_Constant unsigned char, unsigned int)">;
def tcvtrowps2pbf16l : X86Builtin<"_Vector<32, __bf16>(_Constant unsigned char, unsigned int)">;
def tcvtrowps2bf16h : X86Builtin<"_Vector<32, __bf16>(_Constant unsigned char, unsigned int)">;
def tcvtrowps2bf16l : X86Builtin<"_Vector<32, __bf16>(_Constant unsigned char, unsigned int)">;
def tcvtrowps2phh : X86Builtin<"_Vector<32, _Float16>(_Constant unsigned char, unsigned int)">;
def tcvtrowps2phl : X86Builtin<"_Vector<32, _Float16>(_Constant unsigned char, unsigned int)">;
def tilemovrow : X86Builtin<"_Vector<16, int>(_Constant unsigned char, unsigned int)">;
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ enum ASTRecordTypes {
CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION = 75,

UPDATE_MODULE_LOCAL_VISIBLE = 76,

UPDATE_TU_LOCAL_VISIBLE = 77,
};

/// Record types used within a source manager block.
Expand Down Expand Up @@ -1340,6 +1342,10 @@ enum DeclCode {
/// only visible from DeclContext in the same module.
DECL_CONTEXT_MODULE_LOCAL_VISIBLE,

/// A record that stores the set of declarations that are only visible
/// to the TU.
DECL_CONTEXT_TU_LOCAL_VISIBLE,

/// A LabelDecl record.
DECL_LABEL,

Expand Down
20 changes: 19 additions & 1 deletion clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ class ASTReader
uint64_t LexicalOffset;
uint64_t VisibleOffset;
uint64_t ModuleLocalOffset;
uint64_t TULocalOffset;
};

using DelayedNamespaceOffsetMapTy =
Expand Down Expand Up @@ -640,6 +641,9 @@ class ASTReader
llvm::DenseMap<const DeclContext *,
serialization::reader::ModuleLocalLookupTable>
ModuleLocalLookups;
llvm::DenseMap<const DeclContext *,
serialization::reader::DeclContextLookupTable>
TULocalLookups;

using SpecLookupTableTy =
llvm::DenseMap<const Decl *,
Expand Down Expand Up @@ -670,6 +674,7 @@ class ASTReader
llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> PendingVisibleUpdates;
llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates>
PendingModuleLocalVisibleUpdates;
llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> TULocalUpdates;

using SpecializationsUpdate = SmallVector<UpdateData, 1>;
using SpecializationsUpdateMap =
Expand Down Expand Up @@ -704,11 +709,17 @@ class ASTReader
llvm::BitstreamCursor &Cursor,
uint64_t Offset, DeclContext *DC);

enum class VisibleDeclContextStorageKind {
GenerallyVisible,
ModuleLocalVisible,
TULocalVisible,
};

/// Read the record that describes the visible contents of a DC.
bool ReadVisibleDeclContextStorage(ModuleFile &M,
llvm::BitstreamCursor &Cursor,
uint64_t Offset, GlobalDeclID ID,
bool IsModuleLocal);
VisibleDeclContextStorageKind VisibleKind);

bool ReadSpecializations(ModuleFile &M, llvm::BitstreamCursor &Cursor,
uint64_t Offset, Decl *D, bool IsPartial);
Expand Down Expand Up @@ -1148,6 +1159,10 @@ class ASTReader
unsigned NumModuleLocalVisibleDeclContexts = 0,
TotalModuleLocalVisibleDeclContexts = 0;

/// Number of TU Local decl contexts read/total
unsigned NumTULocalVisibleDeclContexts = 0,
TotalTULocalVisibleDeclContexts = 0;

/// Total size of modules, in bits, currently loaded
uint64_t TotalModulesSizeInBits = 0;

Expand Down Expand Up @@ -1481,6 +1496,9 @@ class ASTReader
const serialization::reader::ModuleLocalLookupTable *
getModuleLocalLookupTables(DeclContext *Primary) const;

const serialization::reader::DeclContextLookupTable *
getTULocalLookupTables(DeclContext *Primary) const;

/// Get the loaded specializations lookup tables for \p D,
/// if any.
serialization::reader::LazySpecializationInfoLookupTable *
Expand Down
11 changes: 9 additions & 2 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ class ASTWriter : public ASTDeserializationListener,
/// file.
unsigned NumModuleLocalDeclContexts = 0;

/// The number of TULocal declcontexts written to the AST file.
unsigned NumTULocalDeclContexts = 0;

/// A mapping from each known submodule to its ID number, which will
/// be a positive integer.
llvm::DenseMap<const Module *, unsigned> SubmoduleIDs;
Expand Down Expand Up @@ -594,12 +597,14 @@ class ASTWriter : public ASTDeserializationListener,
void
GenerateNameLookupTable(ASTContext &Context, const DeclContext *DC,
llvm::SmallVectorImpl<char> &LookupTable,
llvm::SmallVectorImpl<char> &ModuleLocalLookupTable);
llvm::SmallVectorImpl<char> &ModuleLocalLookupTable,
llvm::SmallVectorImpl<char> &TULocalLookupTable);
uint64_t WriteDeclContextLexicalBlock(ASTContext &Context,
const DeclContext *DC);
void WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC,
uint64_t &VisibleBlockOffset,
uint64_t &ModuleLocalBlockOffset);
uint64_t &ModuleLocalBlockOffset,
uint64_t &TULocalBlockOffset);
void WriteTypeDeclOffsets();
void WriteFileDeclIDsMap();
void WriteComments(ASTContext &Context);
Expand Down Expand Up @@ -633,8 +638,10 @@ class ASTWriter : public ASTDeserializationListener,
unsigned DeclContextLexicalAbbrev = 0;
unsigned DeclContextVisibleLookupAbbrev = 0;
unsigned DeclModuleLocalVisibleLookupAbbrev = 0;
unsigned DeclTULocalLookupAbbrev = 0;
unsigned UpdateVisibleAbbrev = 0;
unsigned ModuleLocalUpdateVisibleAbbrev = 0;
unsigned TULocalUpdateVisibleAbbrev = 0;
unsigned DeclRecordAbbrev = 0;
unsigned DeclTypedefAbbrev = 0;
unsigned DeclVarAbbrev = 0;
Expand Down
Loading

0 comments on commit 49cc9bb

Please sign in to comment.