Skip to content

Commit

Permalink
Merge pull request #463 from Xilinx/bump_to_95c2d798
Browse files Browse the repository at this point in the history
[AutoBump] Merge with fixes of 95c2d79 (Oct 30) (6)
  • Loading branch information
jorickert authored Feb 13, 2025
2 parents 5346a56 + 03336be commit 54b4bfb
Show file tree
Hide file tree
Showing 2,706 changed files with 108,121 additions and 31,505 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/libcxx-build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
path: |
**/test-results.xml
**/*.abilist
**/CMakeConfigureLog.yaml
**/CMakeError.log
**/CMakeOutput.log
**/crash_diagnostics/*
Expand Down Expand Up @@ -123,6 +124,7 @@ jobs:
path: |
**/test-results.xml
**/*.abilist
**/CMakeConfigureLog.yaml
**/CMakeError.log
**/CMakeOutput.log
**/crash_diagnostics/*
Expand Down Expand Up @@ -188,6 +190,7 @@ jobs:
path: |
**/test-results.xml
**/*.abilist
**/CMakeConfigureLog.yaml
**/CMakeError.log
**/CMakeOutput.log
**/crash_diagnostics/*
Expand Down Expand Up @@ -230,6 +233,7 @@ jobs:
path: |
**/test-results.xml
**/*.abilist
**/CMakeConfigureLog.yaml
**/CMakeError.log
**/CMakeOutput.log
**/crash_diagnostics/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "ReturnConstRefFromParameterCheck.h"
#include "clang/AST/Expr.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"

Expand All @@ -15,19 +16,24 @@ using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {

void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
const auto DRef = ignoringParens(
declRefExpr(
to(parmVarDecl(hasType(hasCanonicalType(
qualType(lValueReferenceType(pointee(
qualType(isConstQualified()))))
.bind("type"))))
.bind("param")))
.bind("dref"));
const auto Func =
functionDecl(hasReturnTypeLoc(loc(
qualType(hasCanonicalType(equalsBoundNode("type"))))))
.bind("func");

Finder->addMatcher(returnStmt(hasReturnValue(DRef), hasAncestor(Func)), this);
Finder->addMatcher(
returnStmt(
hasReturnValue(declRefExpr(
to(parmVarDecl(hasType(hasCanonicalType(
qualType(lValueReferenceType(pointee(
qualType(isConstQualified()))))
.bind("type"))))
.bind("param")))),
hasAncestor(
functionDecl(hasReturnTypeLoc(loc(qualType(
hasCanonicalType(equalsBoundNode("type"))))))
.bind("func")))
.bind("ret"),
returnStmt(hasReturnValue(ignoringParens(conditionalOperator(
eachOf(hasTrueExpression(DRef), hasFalseExpression(DRef)),
hasAncestor(Func))))),
this);
}

Expand Down Expand Up @@ -85,8 +91,8 @@ void ReturnConstRefFromParameterCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func");
const auto *PD = Result.Nodes.getNodeAs<ParmVarDecl>("param");
const auto *R = Result.Nodes.getNodeAs<ReturnStmt>("ret");
const SourceRange Range = R->getRetValue()->getSourceRange();
const auto *DRef = Result.Nodes.getNodeAs<DeclRefExpr>("dref");
const SourceRange Range = DRef->getSourceRange();
if (Range.isInvalid())
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct CognitiveComplexity final {
// Limit of 25 is the "upstream"'s default.
static constexpr unsigned DefaultLimit = 25U;

// Based on the publicly-avaliable numbers for some big open-source projects
// Based on the publicly-available numbers for some big open-source projects
// https://sonarcloud.io/projects?languages=c%2Ccpp&size=5 we can estimate:
// value ~20 would result in no allocs for 98% of functions, ~12 for 96%, ~10
// for 91%, ~8 for 88%, ~6 for 84%, ~4 for 77%, ~2 for 64%, and ~1 for 37%.
Expand Down
63 changes: 41 additions & 22 deletions clang-tools-extra/clangd/ModulesBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/InMemoryModuleCache.h"

namespace clang {
namespace clangd {
Expand Down Expand Up @@ -127,50 +128,68 @@ struct ModuleFile {
std::string ModuleFilePath;
};

bool IsModuleFileUpToDate(
PathRef ModuleFilePath,
const PrerequisiteModules &RequisiteModules) {
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions());

bool IsModuleFileUpToDate(PathRef ModuleFilePath,
const PrerequisiteModules &RequisiteModules,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
auto HSOpts = std::make_shared<HeaderSearchOptions>();
RequisiteModules.adjustHeaderSearchOptions(*HSOpts);
HSOpts->ForceCheckCXX20ModulesInputFiles = true;
HSOpts->ValidateASTInputFilesContent = true;

clang::clangd::IgnoreDiagnostics IgnoreDiags;
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions, &IgnoreDiags,
/*ShouldOwnClient=*/false);

LangOptions LangOpts;
LangOpts.SkipODRCheckInGMF = true;

FileManager FileMgr(FileSystemOptions(), VFS);

SourceManager SourceMgr(*Diags, FileMgr);

HeaderSearch HeaderInfo(HSOpts, SourceMgr, *Diags, LangOpts,
/*Target=*/nullptr);

TrivialModuleLoader ModuleLoader;
Preprocessor PP(std::make_shared<PreprocessorOptions>(), *Diags, LangOpts,
SourceMgr, HeaderInfo, ModuleLoader);

IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache = new InMemoryModuleCache;
PCHContainerOperations PCHOperations;
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
ModuleFilePath.str(), PCHOperations.getRawReader(), ASTUnit::LoadASTOnly,
Diags, FileSystemOptions(), std::move(HSOpts));
ASTReader Reader(PP, *ModuleCache, /*ASTContext=*/nullptr,
PCHOperations.getRawReader(), {});

if (!Unit)
return false;
// We don't need any listener here. By default it will use a validator
// listener.
Reader.setListener(nullptr);

auto Reader = Unit->getASTReader();
if (!Reader)
if (Reader.ReadAST(ModuleFilePath, serialization::MK_MainFile,
SourceLocation(),
ASTReader::ARR_None) != ASTReader::Success)
return false;

bool UpToDate = true;
Reader->getModuleManager().visit([&](serialization::ModuleFile &MF) -> bool {
Reader->visitInputFiles(
Reader.getModuleManager().visit([&](serialization::ModuleFile &MF) -> bool {
Reader.visitInputFiles(
MF, /*IncludeSystem=*/false, /*Complain=*/false,
[&](const serialization::InputFile &IF, bool isSystem) {
if (!IF.getFile() || IF.isOutOfDate())
UpToDate = false;
});

return !UpToDate;
});

return UpToDate;
}

bool IsModuleFilesUpToDate(
llvm::SmallVector<PathRef> ModuleFilePaths,
const PrerequisiteModules &RequisiteModules) {
return llvm::all_of(ModuleFilePaths, [&RequisiteModules](auto ModuleFilePath) {
return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules);
});
const PrerequisiteModules &RequisiteModules,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
return llvm::all_of(
ModuleFilePaths, [&RequisiteModules, VFS](auto ModuleFilePath) {
return IsModuleFileUpToDate(ModuleFilePath, RequisiteModules, VFS);
});
}

// StandalonePrerequisiteModules - stands for PrerequisiteModules for which all
Expand Down Expand Up @@ -347,7 +366,7 @@ bool StandalonePrerequisiteModules::canReuse(
SmallVector<StringRef> BMIPaths;
for (auto &MF : RequiredModules)
BMIPaths.push_back(MF.ModuleFilePath);
return IsModuleFilesUpToDate(BMIPaths, *this);
return IsModuleFilesUpToDate(BMIPaths, *this, VFS);
}

} // namespace clangd
Expand Down
58 changes: 28 additions & 30 deletions clang-tools-extra/clangd/index/SymbolCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ class SymbolCollector::HeaderFileURICache {
}

struct FrameworkHeaderPath {
// Path to the framework directory containing the Headers/PrivateHeaders
// directories e.g. /Frameworks/Foundation.framework/
llvm::StringRef HeadersParentDir;
// Path to the frameworks directory containing the .framework directory.
llvm::StringRef FrameworkParentDir;
// Name of the framework.
llvm::StringRef FrameworkName;
// Subpath relative to the Headers or PrivateHeaders dir, e.g. NSObject.h
// Note: This is NOT relative to the `HeadersParentDir`.
llvm::StringRef HeaderSubpath;
Expand All @@ -351,19 +352,17 @@ class SymbolCollector::HeaderFileURICache {
path::reverse_iterator I = path::rbegin(Path);
path::reverse_iterator Prev = I;
path::reverse_iterator E = path::rend(Path);
FrameworkHeaderPath HeaderPath;
while (I != E) {
if (*I == "Headers") {
FrameworkHeaderPath HeaderPath;
HeaderPath.HeadersParentDir = Path.substr(0, I - E);
if (*I == "Headers" || *I == "PrivateHeaders") {
HeaderPath.HeaderSubpath = Path.substr(Prev - E);
HeaderPath.IsPrivateHeader = false;
return HeaderPath;
}
if (*I == "PrivateHeaders") {
FrameworkHeaderPath HeaderPath;
HeaderPath.HeadersParentDir = Path.substr(0, I - E);
HeaderPath.HeaderSubpath = Path.substr(Prev - E);
HeaderPath.IsPrivateHeader = true;
HeaderPath.IsPrivateHeader = *I == "PrivateHeaders";
if (++I == E)
break;
HeaderPath.FrameworkName = *I;
if (!HeaderPath.FrameworkName.consume_back(".framework"))
break;
HeaderPath.FrameworkParentDir = Path.substr(0, I - E);
return HeaderPath;
}
Prev = I;
Expand All @@ -379,26 +378,27 @@ class SymbolCollector::HeaderFileURICache {
// <Foundation/NSObject_Private.h> which should be used instead of directly
// importing the header.
std::optional<std::string>
getFrameworkUmbrellaSpelling(llvm::StringRef Framework,
const HeaderSearch &HS,
getFrameworkUmbrellaSpelling(const HeaderSearch &HS,
FrameworkHeaderPath &HeaderPath) {
StringRef Framework = HeaderPath.FrameworkName;
auto Res = CacheFrameworkToUmbrellaHeaderSpelling.try_emplace(Framework);
auto *CachedSpelling = &Res.first->second;
if (!Res.second) {
return HeaderPath.IsPrivateHeader ? CachedSpelling->PrivateHeader
: CachedSpelling->PublicHeader;
}
SmallString<256> UmbrellaPath(HeaderPath.HeadersParentDir);
llvm::sys::path::append(UmbrellaPath, "Headers", Framework + ".h");
SmallString<256> UmbrellaPath(HeaderPath.FrameworkParentDir);
llvm::sys::path::append(UmbrellaPath, Framework + ".framework", "Headers",
Framework + ".h");

llvm::vfs::Status Status;
auto StatErr = HS.getFileMgr().getNoncachedStatValue(UmbrellaPath, Status);
if (!StatErr)
CachedSpelling->PublicHeader = llvm::formatv("<{0}/{0}.h>", Framework);

UmbrellaPath = HeaderPath.HeadersParentDir;
llvm::sys::path::append(UmbrellaPath, "PrivateHeaders",
Framework + "_Private.h");
UmbrellaPath = HeaderPath.FrameworkParentDir;
llvm::sys::path::append(UmbrellaPath, Framework + ".framework",
"PrivateHeaders", Framework + "_Private.h");

StatErr = HS.getFileMgr().getNoncachedStatValue(UmbrellaPath, Status);
if (!StatErr)
Expand All @@ -414,8 +414,7 @@ class SymbolCollector::HeaderFileURICache {
// give <Foundation/Foundation.h> if the umbrella header exists, otherwise
// <Foundation/NSObject.h>.
std::optional<llvm::StringRef>
getFrameworkHeaderIncludeSpelling(FileEntryRef FE, llvm::StringRef Framework,
HeaderSearch &HS) {
getFrameworkHeaderIncludeSpelling(FileEntryRef FE, HeaderSearch &HS) {
auto Res = CachePathToFrameworkSpelling.try_emplace(FE.getName());
auto *CachedHeaderSpelling = &Res.first->second;
if (!Res.second)
Expand All @@ -429,13 +428,15 @@ class SymbolCollector::HeaderFileURICache {
return std::nullopt;
}
if (auto UmbrellaSpelling =
getFrameworkUmbrellaSpelling(Framework, HS, *HeaderPath)) {
getFrameworkUmbrellaSpelling(HS, *HeaderPath)) {
*CachedHeaderSpelling = *UmbrellaSpelling;
return llvm::StringRef(*CachedHeaderSpelling);
}

*CachedHeaderSpelling =
llvm::formatv("<{0}/{1}>", Framework, HeaderPath->HeaderSubpath).str();
llvm::formatv("<{0}/{1}>", HeaderPath->FrameworkName,
HeaderPath->HeaderSubpath)
.str();
return llvm::StringRef(*CachedHeaderSpelling);
}

Expand All @@ -454,11 +455,8 @@ class SymbolCollector::HeaderFileURICache {
// Framework headers are spelled as <FrameworkName/Foo.h>, not
// "path/FrameworkName.framework/Headers/Foo.h".
auto &HS = PP->getHeaderSearchInfo();
if (const auto *HFI = HS.getExistingFileInfo(*FE))
if (!HFI->Framework.empty())
if (auto Spelling =
getFrameworkHeaderIncludeSpelling(*FE, HFI->Framework, HS))
return *Spelling;
if (auto Spelling = getFrameworkHeaderIncludeSpelling(*FE, HS))
return *Spelling;

if (!tooling::isSelfContainedHeader(*FE, PP->getSourceManager(),
PP->getHeaderSearchInfo())) {
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ Changes in existing checks
<clang-tidy/checks/bugprone/posix-return>` check to support integer literals
as LHS and posix call as RHS of comparison.

- Improved :doc:`bugprone-return-const-ref-from-parameter
<clang-tidy/checks/bugprone/return-const-ref-from-parameter>` check to
diagnose potential dangling references when returning a ``const &`` parameter
by using the conditional operator ``cond ? var1 : var2``.

- Improved :doc:`bugprone-sizeof-expression
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
usages of ``sizeof()``, ``alignof()``, and ``offsetof()`` when adding or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ int const &f3(TConstRef a) { return a; }
int const &f4(TConst &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter

int const &f5(TConst &a) { return true ? a : a; }
// CHECK-MESSAGES: :[[@LINE-1]]:42: warning: returning a constant reference parameter
// CHECK-MESSAGES: :[[@LINE-2]]:46: warning: returning a constant reference parameter

template <typename T>
const T& tf1(const T &a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: returning a constant reference parameter
Expand All @@ -47,6 +51,11 @@ template <typename T>
const T& itf4(typename ConstRef<T>::type a) { return a; }
// CHECK-MESSAGES: :[[@LINE-1]]:54: warning: returning a constant reference parameter

template <typename T>
const T& itf5(const T &a) { return true ? a : a; }
// CHECK-MESSAGES: :[[@LINE-1]]:43: warning: returning a constant reference parameter
// CHECK-MESSAGES: :[[@LINE-2]]:47: warning: returning a constant reference parameter

void instantiate(const int &param, const float &paramf, int &mut_param, float &mut_paramf) {
itf1(0);
itf1(param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ void unittest_false() {
//----------------------------------------------------------------------------//

// break does not increase cognitive complexity.
// only break LABEL does, but it is unavaliable in C or C++
// only break LABEL does, but it is unavailable in C or C++

// continue does not increase cognitive complexity.
// only continue LABEL does, but it is unavaliable in C or C++
// only continue LABEL does, but it is unavailable in C or C++

void unittest_b1_00() {
// CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'unittest_b1_00' has cognitive complexity of 33 (threshold 0) [readability-function-cognitive-complexity]
Expand Down
2 changes: 1 addition & 1 deletion clang/Maintainers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Sema
Experimental new constant interpreter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Timm Bäder
| tbaeder\@redhat.com (em), tbaeder (Phabricator), tbaederr (GitHub), tbaeder (Discourse), tbaeder (Discord)
| tbaeder\@redhat.com (email), tbaeder (Phabricator), tbaederr (GitHub), tbaeder (Discourse), tbaeder (Discord)

Modules & serialization
Expand Down
Loading

0 comments on commit 54b4bfb

Please sign in to comment.