Skip to content

Commit 6ddb94b

Browse files
author
git apple-llvm automerger
committed
Merge commit '874c49f55454' from llvm.org/main into experimental/cas/main
2 parents fb816d0 + 874c49f commit 6ddb94b

File tree

115 files changed

+3361
-1011
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+3361
-1011
lines changed

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,16 +605,15 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
605605
// Copy over the macros in the preamble region of the main file, and combine
606606
// with non-preamble macros below.
607607
MainFileMacros Macros;
608-
if (Preamble)
609-
Macros = Preamble->Macros;
608+
std::vector<PragmaMark> Marks;
609+
if (Preamble) {
610+
Macros = Patch->mainFileMacros();
611+
Marks = Patch->marks();
612+
}
610613
Clang->getPreprocessor().addPPCallbacks(
611614
std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(),
612615
Macros));
613616

614-
std::vector<PragmaMark> Marks;
615-
// FIXME: We need to patch the marks for stale preambles.
616-
if (Preamble)
617-
Marks = Preamble->Marks;
618617
Clang->getPreprocessor().addPPCallbacks(
619618
collectPragmaMarksCallback(Clang->getSourceManager(), Marks));
620619

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Preamble.h"
10+
#include "CollectMacros.h"
1011
#include "Compiler.h"
1112
#include "Config.h"
1213
#include "Diagnostics.h"
@@ -765,6 +766,7 @@ PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
765766
return PreamblePatch::unmodified(Baseline);
766767

767768
PreamblePatch PP;
769+
PP.Baseline = &Baseline;
768770
// This shouldn't coincide with any real file name.
769771
llvm::SmallString<128> PatchName;
770772
llvm::sys::path::append(PatchName, llvm::sys::path::parent_path(FileName),
@@ -886,6 +888,7 @@ std::vector<Inclusion> PreamblePatch::preambleIncludes() const {
886888

887889
PreamblePatch PreamblePatch::unmodified(const PreambleData &Preamble) {
888890
PreamblePatch PP;
891+
PP.Baseline = &Preamble;
889892
PP.PreambleIncludes = Preamble.Includes.MainFileIncludes;
890893
PP.ModifiedBounds = Preamble.Preamble.getBounds();
891894
PP.PatchedDiags = Preamble.Diags;
@@ -896,5 +899,18 @@ bool PreamblePatch::preserveDiagnostics() const {
896899
return PatchContents.empty() ||
897900
Config::current().Diagnostics.AllowStalePreamble;
898901
}
902+
llvm::ArrayRef<PragmaMark> PreamblePatch::marks() const {
903+
if (PatchContents.empty())
904+
return Baseline->Marks;
905+
// FIXME: Patch pragma marks.
906+
return {};
907+
}
908+
909+
MainFileMacros PreamblePatch::mainFileMacros() const {
910+
if (PatchContents.empty())
911+
return Baseline->Macros;
912+
// FIXME: Patch main file macros.
913+
return MainFileMacros();
914+
}
899915
} // namespace clangd
900916
} // namespace clang

clang-tools-extra/clangd/Preamble.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class PreamblePatch {
163163

164164
static constexpr llvm::StringLiteral HeaderName = "__preamble_patch__.h";
165165

166+
llvm::ArrayRef<PragmaMark> marks() const;
167+
MainFileMacros mainFileMacros() const;
168+
166169
private:
167170
static PreamblePatch create(llvm::StringRef FileName,
168171
const ParseInputs &Modified,
@@ -178,6 +181,7 @@ class PreamblePatch {
178181
// Diags that were attached to a line preserved in Modified contents.
179182
std::vector<Diag> PatchedDiags;
180183
PreambleBounds ModifiedBounds = {0, false};
184+
const PreambleData *Baseline = nullptr;
181185
};
182186

183187
} // namespace clangd

clang-tools-extra/clangd/unittests/PreambleTests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "TestFS.h"
2020
#include "TestTU.h"
2121
#include "XRefs.h"
22+
#include "support/Context.h"
2223
#include "clang/Basic/SourceManager.h"
2324
#include "clang/Format/Format.h"
2425
#include "clang/Frontend/FrontendActions.h"
@@ -36,6 +37,7 @@
3637
#include <memory>
3738
#include <optional>
3839
#include <string>
40+
#include <utility>
3941
#include <vector>
4042

4143
using testing::AllOf;
@@ -826,6 +828,35 @@ x>)");
826828
EXPECT_THAT(*AST->getDiagnostics(), IsEmpty());
827829
}
828830
}
831+
832+
TEST(PreamblePatch, MacroAndMarkHandling) {
833+
Config Cfg;
834+
Cfg.Diagnostics.AllowStalePreamble = true;
835+
WithContextValue WithCfg(Config::Key, std::move(Cfg));
836+
837+
{
838+
Annotations Code(R"cpp(
839+
#ifndef FOO
840+
#define FOO
841+
// Some comments
842+
#pragma mark XX
843+
#define BAR
844+
845+
#endif)cpp");
846+
Annotations NewCode(R"cpp(
847+
#ifndef FOO
848+
#define FOO
849+
#define BAR
850+
#pragma mark XX
851+
852+
#endif)cpp");
853+
auto AST = createPatchedAST(Code.code(), NewCode.code());
854+
// FIXME: Macros and marks have locations that need to be patched.
855+
EXPECT_THAT(AST->getMacros().Names, IsEmpty());
856+
EXPECT_THAT(AST->getMarks(), IsEmpty());
857+
}
858+
}
859+
829860
} // namespace
830861
} // namespace clangd
831862
} // namespace clang

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Improvements to Clang's diagnostics
167167
``<built-in>``.
168168
- Clang constexpr evaluator now provides a more concise diagnostic when calling
169169
function pointer that is known to be null.
170+
- Clang now avoids duplicate warnings on unreachable ``[[fallthrough]];`` statements
171+
previously issued from ``-Wunreachable-code`` and ``-Wunreachable-code-fallthrough``
172+
by prioritizing ``-Wunreachable-code-fallthrough``.
170173

171174
Bug Fixes in This Version
172175
-------------------------
@@ -303,11 +306,6 @@ libclang
303306
- Introduced the new function ``clang_CXXMethod_isExplicit``,
304307
which identifies whether a constructor or conversion function cursor
305308
was marked with the explicit identifier.
306-
- Added check in ``clang_getFieldDeclBitWidth`` for whether a bit field
307-
has an evaluable bit width. Fixes undefined behavior when called on a
308-
bit field whose width depends on a template paramter.
309-
- Added function ``clang_isBitFieldDecl`` to check if a struct/class field is a
310-
bit field.
311309

312310
- Introduced the new ``CXIndex`` constructor function
313311
``clang_createIndexWithOptions``, which allows overriding precompiled preamble

clang/include/clang-c/Index.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,18 +3028,10 @@ CINDEX_LINKAGE long long clang_getEnumConstantDeclValue(CXCursor C);
30283028
CINDEX_LINKAGE unsigned long long
30293029
clang_getEnumConstantDeclUnsignedValue(CXCursor C);
30303030

3031-
/**
3032-
* Returns non-zero if a field declaration has a bit width expression.
3033-
*
3034-
* If the cursor does not reference a bit field declaration 0 is returned.
3035-
*/
3036-
CINDEX_LINKAGE unsigned clang_isBitFieldDecl(CXCursor C);
3037-
30383031
/**
30393032
* Retrieve the bit width of a bit field declaration as an integer.
30403033
*
3041-
* If the cursor does not reference a bit field, or if the bit field's width
3042-
* expression cannot be evaluated, -1 is returned.
3034+
* If a cursor that is not a bit field declaration is passed in, -1 is returned.
30433035
*/
30443036
CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor C);
30453037

clang/include/clang/Analysis/Analyses/ReachableCode.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ class Callback {
4848
virtual void anchor();
4949
public:
5050
virtual ~Callback() {}
51-
virtual void HandleUnreachable(UnreachableKind UK,
52-
SourceLocation L,
53-
SourceRange ConditionVal,
54-
SourceRange R1,
55-
SourceRange R2) = 0;
51+
virtual void HandleUnreachable(UnreachableKind UK, SourceLocation L,
52+
SourceRange ConditionVal, SourceRange R1,
53+
SourceRange R2, bool HasFallThroughAttr) = 0;
5654
};
5755

5856
/// ScanReachableFromBlock - Mark all blocks reachable from Start.

clang/include/clang/Sema/Sema.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ class Sema final {
10451045
/// ExpressionEvaluationContextRecord object.
10461046
bool isConstantEvaluatedOverride;
10471047

1048-
bool isConstantEvaluated() {
1048+
bool isConstantEvaluated() const {
10491049
return ExprEvalContexts.back().isConstantEvaluated() ||
10501050
isConstantEvaluatedOverride;
10511051
}
@@ -1486,7 +1486,7 @@ class Sema final {
14861486
/// Determine if VD, which must be a variable or function, is an external
14871487
/// symbol that nonetheless can't be referenced from outside this translation
14881488
/// unit because its type has no linkage and it's not extern "C".
1489-
bool isExternalWithNoLinkageType(ValueDecl *VD);
1489+
bool isExternalWithNoLinkageType(ValueDecl *VD) const;
14901490

14911491
/// Obtain a sorted list of functions that are undefined but ODR-used.
14921492
void getUndefinedButUsed(
@@ -3548,13 +3548,13 @@ class Sema final {
35483548
void ActOnExitFunctionContext();
35493549

35503550
/// If \p AllowLambda is true, treat lambda as function.
3551-
DeclContext *getFunctionLevelDeclContext(bool AllowLambda = false);
3551+
DeclContext *getFunctionLevelDeclContext(bool AllowLambda = false) const;
35523552

35533553
/// Returns a pointer to the innermost enclosing function, or nullptr if the
35543554
/// current context is not inside a function. If \p AllowLambda is true,
35553555
/// this can return the call operator of an enclosing lambda, otherwise
35563556
/// lambdas are skipped when looking for an enclosing function.
3557-
FunctionDecl *getCurFunctionDecl(bool AllowLambda = false);
3557+
FunctionDecl *getCurFunctionDecl(bool AllowLambda = false) const;
35583558

35593559
/// getCurMethodDecl - If inside of a method body, this returns a pointer to
35603560
/// the method decl for the method being parsed. If we're currently
@@ -3564,7 +3564,7 @@ class Sema final {
35643564
/// getCurFunctionOrMethodDecl - Return the Decl for the current ObjC method
35653565
/// or C function we're in, otherwise return null. If we're currently
35663566
/// in a 'block', this returns the containing context.
3567-
NamedDecl *getCurFunctionOrMethodDecl();
3567+
NamedDecl *getCurFunctionOrMethodDecl() const;
35683568

35693569
/// Add this decl to the scope shadowed decl chains.
35703570
void PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext = true);
@@ -3577,7 +3577,7 @@ class Sema final {
35773577
/// enclosing namespace set of the context, rather than contained
35783578
/// directly within it.
35793579
bool isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S = nullptr,
3580-
bool AllowInlineNamespace = false);
3580+
bool AllowInlineNamespace = false) const;
35813581

35823582
/// Finds the scope corresponding to the given decl context, if it
35833583
/// happens to be an enclosing scope. Otherwise return NULL.
@@ -4349,7 +4349,7 @@ class Sema final {
43494349
ForExternalRedeclaration
43504350
};
43514351

4352-
RedeclarationKind forRedeclarationInCurContext() {
4352+
RedeclarationKind forRedeclarationInCurContext() const {
43534353
// A declaration with an owning module for linkage can never link against
43544354
// anything that is not visible. We don't need to check linkage here; if
43554355
// the context has internal linkage, redeclaration lookup won't find things

clang/lib/Analysis/ReachableCode.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "clang/Analysis/Analyses/ReachableCode.h"
15+
#include "clang/AST/Attr.h"
1516
#include "clang/AST/Expr.h"
1617
#include "clang/AST/ExprCXX.h"
1718
#include "clang/AST/ExprObjC.h"
@@ -629,6 +630,10 @@ void DeadCodeScan::reportDeadCode(const CFGBlock *B,
629630
UK = reachable_code::UK_Return;
630631
}
631632

633+
const auto *AS = dyn_cast<AttributedStmt>(S);
634+
bool HasFallThroughAttr =
635+
AS && hasSpecificAttr<FallThroughAttr>(AS->getAttrs());
636+
632637
SourceRange SilenceableCondVal;
633638

634639
if (UK == reachable_code::UK_Other) {
@@ -645,8 +650,9 @@ void DeadCodeScan::reportDeadCode(const CFGBlock *B,
645650
R2 = Inc->getSourceRange();
646651
}
647652

648-
CB.HandleUnreachable(reachable_code::UK_Loop_Increment,
649-
Loc, SourceRange(), SourceRange(Loc, Loc), R2);
653+
CB.HandleUnreachable(reachable_code::UK_Loop_Increment, Loc,
654+
SourceRange(), SourceRange(Loc, Loc), R2,
655+
HasFallThroughAttr);
650656
return;
651657
}
652658

@@ -665,7 +671,7 @@ void DeadCodeScan::reportDeadCode(const CFGBlock *B,
665671

666672
SourceRange R1, R2;
667673
SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
668-
CB.HandleUnreachable(UK, Loc, SilenceableCondVal, R1, R2);
674+
CB.HandleUnreachable(UK, Loc, SilenceableCondVal, R1, R2, HasFallThroughAttr);
669675
}
670676

671677
//===----------------------------------------------------------------------===//

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,7 +2400,7 @@ class AnnotatingParser {
24002400

24012401
if (!NextToken ||
24022402
NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept, tok::comma,
2403-
tok::r_paren) ||
2403+
tok::r_paren, TT_RequiresClause) ||
24042404
NextToken->canBePointerOrReferenceQualifier() ||
24052405
(NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
24062406
return TT_PointerOrReference;
@@ -3758,8 +3758,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
37583758
if (Right.is(TT_BlockComment))
37593759
return true;
37603760
// foo() -> const Bar * override/final
3761-
if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final,
3762-
tok::kw_noexcept) &&
3761+
// S::foo() & noexcept/requires
3762+
if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
3763+
TT_RequiresClause) &&
37633764
!Right.is(TT_StartOfName)) {
37643765
return true;
37653766
}

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,17 @@ namespace {
6666
public:
6767
UnreachableCodeHandler(Sema &s) : S(s) {}
6868

69-
void HandleUnreachable(reachable_code::UnreachableKind UK,
70-
SourceLocation L,
71-
SourceRange SilenceableCondVal,
72-
SourceRange R1,
73-
SourceRange R2) override {
69+
void HandleUnreachable(reachable_code::UnreachableKind UK, SourceLocation L,
70+
SourceRange SilenceableCondVal, SourceRange R1,
71+
SourceRange R2, bool HasFallThroughAttr) override {
72+
// If the diagnosed code is `[[fallthrough]];` and
73+
// `-Wunreachable-code-fallthrough` is enabled, suppress `code will never
74+
// be executed` warning to avoid generating diagnostic twice
75+
if (HasFallThroughAttr &&
76+
!S.getDiagnostics().isIgnored(diag::warn_unreachable_fallthrough_attr,
77+
SourceLocation()))
78+
return;
79+
7480
// Avoid reporting multiple unreachable code diagnostics that are
7581
// triggered by the same conditional value.
7682
if (PreviousSilenceableCondVal.isValid() &&

clang/lib/Sema/Sema.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ static bool isFunctionOrVarDeclExternC(NamedDecl *ND) {
793793

794794
/// Determine whether ND is an external-linkage function or variable whose
795795
/// type has no linkage.
796-
bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) {
796+
bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) const {
797797
// Note: it's not quite enough to check whether VD has UniqueExternalLinkage,
798798
// because we also want to catch the case where its type has VisibleNoLinkage,
799799
// which does not affect the linkage of VD.
@@ -1441,7 +1441,7 @@ void Sema::ActOnEndOfTranslationUnit() {
14411441
// Helper functions.
14421442
//===----------------------------------------------------------------------===//
14431443

1444-
DeclContext *Sema::getFunctionLevelDeclContext(bool AllowLambda) {
1444+
DeclContext *Sema::getFunctionLevelDeclContext(bool AllowLambda) const {
14451445
DeclContext *DC = CurContext;
14461446

14471447
while (true) {
@@ -1461,7 +1461,7 @@ DeclContext *Sema::getFunctionLevelDeclContext(bool AllowLambda) {
14611461
/// getCurFunctionDecl - If inside of a function body, this returns a pointer
14621462
/// to the function decl for the function being parsed. If we're currently
14631463
/// in a 'block', this returns the containing context.
1464-
FunctionDecl *Sema::getCurFunctionDecl(bool AllowLambda) {
1464+
FunctionDecl *Sema::getCurFunctionDecl(bool AllowLambda) const {
14651465
DeclContext *DC = getFunctionLevelDeclContext(AllowLambda);
14661466
return dyn_cast<FunctionDecl>(DC);
14671467
}
@@ -1473,7 +1473,7 @@ ObjCMethodDecl *Sema::getCurMethodDecl() {
14731473
return dyn_cast<ObjCMethodDecl>(DC);
14741474
}
14751475

1476-
NamedDecl *Sema::getCurFunctionOrMethodDecl() {
1476+
NamedDecl *Sema::getCurFunctionOrMethodDecl() const {
14771477
DeclContext *DC = getFunctionLevelDeclContext();
14781478
if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
14791479
return cast<NamedDecl>(DC);

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) {
15931593
}
15941594

15951595
bool Sema::isDeclInScope(NamedDecl *D, DeclContext *Ctx, Scope *S,
1596-
bool AllowInlineNamespace) {
1596+
bool AllowInlineNamespace) const {
15971597
return IdResolver.isDeclInScope(D, Ctx, S, AllowInlineNamespace);
15981598
}
15991599

0 commit comments

Comments
 (0)