Skip to content

Commit 92d8457

Browse files
committed
Merge from 'main' to 'sycl-web' (#11)
CONFLICT (content): Merge conflict in clang/lib/CodeGen/CodeGenFunction.cpp
2 parents 2d55bdf + ea864c9 commit 92d8457

File tree

604 files changed

+25244
-21838
lines changed

Some content is hidden

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

604 files changed

+25244
-21838
lines changed

clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
127127
"in a header file; function definitions in header files can lead to "
128128
"ODR violations")
129129
<< IsFullSpec << FD;
130+
// inline is not allowed for main function.
131+
if (FD->isMain())
132+
return;
130133
diag(FD->getLocation(), /*FixDescription=*/"make as 'inline'",
131134
DiagnosticIDs::Note)
132135
<< FixItHint::CreateInsertion(FD->getInnerLocStart(), "inline ");

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,16 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
309309
MainInput.getFile());
310310
return None;
311311
}
312+
// If we saw an include guard in the preamble section of the main file,
313+
// mark the main-file as include-guarded.
314+
// This information is part of the HeaderFileInfo but is not loaded from the
315+
// preamble as the file's size is part of its identity and may have changed.
316+
// (The rest of HeaderFileInfo is not relevant for our purposes).
317+
if (Preamble && Preamble->MainIsIncludeGuarded) {
318+
const SourceManager &SM = Clang->getSourceManager();
319+
const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
320+
Clang->getPreprocessor().getHeaderSearchInfo().MarkFileIncludeOnce(MainFE);
321+
}
312322

313323
// Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
314324
// Clang-tidy has some limitations to ensure reasonable performance:

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,20 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
7575

7676
CanonicalIncludes takeCanonicalIncludes() { return std::move(CanonIncludes); }
7777

78+
bool isMainFileIncludeGuarded() const { return IsMainFileIncludeGuarded; }
79+
7880
void AfterExecute(CompilerInstance &CI) override {
79-
if (!ParsedCallback)
80-
return;
81-
trace::Span Tracer("Running PreambleCallback");
82-
ParsedCallback(CI.getASTContext(), CI.getPreprocessorPtr(), CanonIncludes);
81+
if (ParsedCallback) {
82+
trace::Span Tracer("Running PreambleCallback");
83+
ParsedCallback(CI.getASTContext(), CI.getPreprocessorPtr(),
84+
CanonIncludes);
85+
}
86+
87+
const SourceManager &SM = CI.getSourceManager();
88+
const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
89+
IsMainFileIncludeGuarded =
90+
CI.getPreprocessor().getHeaderSearchInfo().isFileMultipleIncludeGuarded(
91+
MainFE);
8392
}
8493

8594
void BeforeExecute(CompilerInstance &CI) override {
@@ -121,6 +130,7 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
121130
IncludeStructure Includes;
122131
CanonicalIncludes CanonIncludes;
123132
MainFileMacros Macros;
133+
bool IsMainFileIncludeGuarded = false;
124134
std::unique_ptr<CommentHandler> IWYUHandler = nullptr;
125135
const clang::LangOptions *LangOpts = nullptr;
126136
const SourceManager *SourceMgr = nullptr;
@@ -365,16 +375,15 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
365375
// to read back. We rely on dynamic index for the comments instead.
366376
CI.getPreprocessorOpts().WriteCommentListToPCH = false;
367377

368-
CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback);
378+
CppFilePreambleCallbacks CapturedInfo(FileName, PreambleCallback);
369379
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
370380
llvm::SmallString<32> AbsFileName(FileName);
371381
VFS->makeAbsolute(AbsFileName);
372382
auto StatCache = std::make_unique<PreambleFileStatusCache>(AbsFileName);
373383
auto BuiltPreamble = PrecompiledPreamble::Build(
374384
CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine,
375385
StatCache->getProducingFS(VFS),
376-
std::make_shared<PCHContainerOperations>(), StoreInMemory,
377-
SerializedDeclsCollector);
386+
std::make_shared<PCHContainerOperations>(), StoreInMemory, CapturedInfo);
378387

379388
// When building the AST for the main file, we do want the function
380389
// bodies.
@@ -384,16 +393,17 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
384393
vlog("Built preamble of size {0} for file {1} version {2}",
385394
BuiltPreamble->getSize(), FileName, Inputs.Version);
386395
std::vector<Diag> Diags = PreambleDiagnostics.take();
387-
return std::make_shared<PreambleData>(
396+
auto Result = std::make_shared<PreambleData>(
388397
Inputs, std::move(*BuiltPreamble), std::move(Diags),
389-
SerializedDeclsCollector.takeIncludes(),
390-
SerializedDeclsCollector.takeMacros(), std::move(StatCache),
391-
SerializedDeclsCollector.takeCanonicalIncludes());
392-
} else {
393-
elog("Could not build a preamble for file {0} version {1}: {2}", FileName,
394-
Inputs.Version, BuiltPreamble.getError().message());
395-
return nullptr;
398+
CapturedInfo.takeIncludes(), CapturedInfo.takeMacros(),
399+
std::move(StatCache), CapturedInfo.takeCanonicalIncludes());
400+
Result->MainIsIncludeGuarded = CapturedInfo.isMainFileIncludeGuarded();
401+
return Result;
396402
}
403+
404+
elog("Could not build a preamble for file {0} version {1}: {2}", FileName,
405+
Inputs.Version, BuiltPreamble.getError().message());
406+
return nullptr;
397407
}
398408

399409
bool isPreambleCompatible(const PreambleData &Preamble,

clang-tools-extra/clangd/Preamble.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ struct PreambleData {
6969
// When reusing a preamble, this cache can be consumed to save IO.
7070
std::unique_ptr<PreambleFileStatusCache> StatCache;
7171
CanonicalIncludes CanonIncludes;
72+
// Whether there was a (possibly-incomplete) include-guard on the main file.
73+
// We need to propagate this information "by hand" to subsequent parses.
74+
bool MainIsIncludeGuarded = false;
7275
};
7376

7477
using PreambleParsedCallback =

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

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace {
4646
using ::testing::AllOf;
4747
using ::testing::ElementsAre;
4848
using ::testing::ElementsAreArray;
49+
using ::testing::IsEmpty;
4950

5051
MATCHER_P(DeclNamed, Name, "") {
5152
if (NamedDecl *ND = dyn_cast<NamedDecl>(arg))
@@ -633,6 +634,258 @@ TEST(ParsedASTTest, PatchesDeletedIncludes) {
633634
testPath("foo.cpp"))));
634635
}
635636

637+
// Returns Code guarded by #ifndef guards
638+
std::string guard(llvm::StringRef Code) {
639+
static int GuardID = 0;
640+
std::string GuardName = ("GUARD_" + llvm::Twine(++GuardID)).str();
641+
return llvm::formatv("#ifndef {0}\n#define {0}\n{1}\n#endif\n", GuardName,
642+
Code);
643+
}
644+
645+
std::string once(llvm::StringRef Code) {
646+
return llvm::formatv("#pragma once\n{0}\n", Code);
647+
}
648+
649+
bool mainIsGuarded(const ParsedAST &AST) {
650+
const auto &SM = AST.getSourceManager();
651+
const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID());
652+
return AST.getPreprocessor()
653+
.getHeaderSearchInfo()
654+
.isFileMultipleIncludeGuarded(MainFE);
655+
}
656+
657+
MATCHER_P(Diag, Desc, "") {
658+
return llvm::StringRef(arg.Message).contains(Desc);
659+
}
660+
661+
// Check our understanding of whether the main file is header guarded or not.
662+
TEST(ParsedASTTest, HeaderGuards) {
663+
TestTU TU;
664+
TU.ImplicitHeaderGuard = false;
665+
666+
TU.Code = ";";
667+
EXPECT_FALSE(mainIsGuarded(TU.build()));
668+
669+
TU.Code = guard(";");
670+
EXPECT_TRUE(mainIsGuarded(TU.build()));
671+
672+
TU.Code = once(";");
673+
EXPECT_TRUE(mainIsGuarded(TU.build()));
674+
675+
TU.Code = R"cpp(
676+
;
677+
#pragma once
678+
)cpp";
679+
EXPECT_FALSE(mainIsGuarded(TU.build())); // FIXME: true
680+
681+
TU.Code = R"cpp(
682+
;
683+
#ifndef GUARD
684+
#define GUARD
685+
;
686+
#endif
687+
)cpp";
688+
EXPECT_FALSE(mainIsGuarded(TU.build()));
689+
}
690+
691+
// Check our handling of files that include themselves.
692+
// Ideally we allow this if the file has header guards.
693+
//
694+
// Note: the semicolons (empty statements) are significant!
695+
// - they force the preamble to end and the body to begin. Directives can have
696+
// different effects in the preamble vs main file (which we try to hide).
697+
// - if the preamble would otherwise cover the whole file, a trailing semicolon
698+
// forces their sizes to be different. This is significant because the file
699+
// size is part of the lookup key for HeaderFileInfo, and we don't want to
700+
// rely on the preamble's HFI being looked up when parsing the main file.
701+
TEST(ParsedASTTest, HeaderGuardsSelfInclude) {
702+
TestTU TU;
703+
TU.ImplicitHeaderGuard = false;
704+
TU.Filename = "self.h";
705+
706+
TU.Code = R"cpp(
707+
#include "self.h" // error-ok
708+
;
709+
)cpp";
710+
auto AST = TU.build();
711+
EXPECT_THAT(*AST.getDiagnostics(),
712+
ElementsAre(Diag("recursively when building a preamble")));
713+
EXPECT_FALSE(mainIsGuarded(AST));
714+
715+
TU.Code = R"cpp(
716+
;
717+
#include "self.h" // error-ok
718+
)cpp";
719+
AST = TU.build();
720+
EXPECT_THAT(*AST.getDiagnostics(), ElementsAre(Diag("nested too deeply")));
721+
EXPECT_FALSE(mainIsGuarded(AST));
722+
723+
TU.Code = R"cpp(
724+
#pragma once
725+
#include "self.h"
726+
;
727+
)cpp";
728+
AST = TU.build();
729+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
730+
EXPECT_TRUE(mainIsGuarded(AST));
731+
732+
TU.Code = R"cpp(
733+
#pragma once
734+
;
735+
#include "self.h"
736+
)cpp";
737+
AST = TU.build();
738+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
739+
EXPECT_TRUE(mainIsGuarded(AST));
740+
741+
TU.Code = R"cpp(
742+
;
743+
#pragma once
744+
#include "self.h"
745+
)cpp";
746+
AST = TU.build();
747+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
748+
EXPECT_TRUE(mainIsGuarded(AST));
749+
750+
TU.Code = R"cpp(
751+
#ifndef GUARD
752+
#define GUARD
753+
#include "self.h" // error-ok: FIXME, this would be nice to support
754+
#endif
755+
;
756+
)cpp";
757+
AST = TU.build();
758+
EXPECT_THAT(*AST.getDiagnostics(),
759+
ElementsAre(Diag("recursively when building a preamble")));
760+
EXPECT_TRUE(mainIsGuarded(AST));
761+
762+
TU.Code = R"cpp(
763+
#ifndef GUARD
764+
#define GUARD
765+
;
766+
#include "self.h"
767+
#endif
768+
)cpp";
769+
AST = TU.build();
770+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
771+
EXPECT_TRUE(mainIsGuarded(AST));
772+
773+
// Guarded too late...
774+
TU.Code = R"cpp(
775+
#include "self.h" // error-ok
776+
#ifndef GUARD
777+
#define GUARD
778+
;
779+
#endif
780+
)cpp";
781+
AST = TU.build();
782+
EXPECT_THAT(*AST.getDiagnostics(),
783+
ElementsAre(Diag("recursively when building a preamble")));
784+
EXPECT_FALSE(mainIsGuarded(AST));
785+
786+
TU.Code = R"cpp(
787+
#include "self.h" // error-ok
788+
;
789+
#ifndef GUARD
790+
#define GUARD
791+
#endif
792+
)cpp";
793+
AST = TU.build();
794+
EXPECT_THAT(*AST.getDiagnostics(),
795+
ElementsAre(Diag("recursively when building a preamble")));
796+
EXPECT_FALSE(mainIsGuarded(AST));
797+
798+
TU.Code = R"cpp(
799+
;
800+
#ifndef GUARD
801+
#define GUARD
802+
#include "self.h"
803+
#endif
804+
)cpp";
805+
AST = TU.build();
806+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
807+
EXPECT_FALSE(mainIsGuarded(AST));
808+
809+
TU.Code = R"cpp(
810+
#include "self.h" // error-ok
811+
#pragma once
812+
;
813+
)cpp";
814+
AST = TU.build();
815+
EXPECT_THAT(*AST.getDiagnostics(),
816+
ElementsAre(Diag("recursively when building a preamble")));
817+
EXPECT_TRUE(mainIsGuarded(AST));
818+
819+
TU.Code = R"cpp(
820+
#include "self.h" // error-ok
821+
;
822+
#pragma once
823+
)cpp";
824+
AST = TU.build();
825+
EXPECT_THAT(*AST.getDiagnostics(),
826+
ElementsAre(Diag("recursively when building a preamble")));
827+
EXPECT_TRUE(mainIsGuarded(AST));
828+
}
829+
830+
// Tests how we handle common idioms for splitting a header-only library
831+
// into interface and implementation files (e.g. *.h vs *.inl).
832+
// These files mutually include each other, and need careful handling of include
833+
// guards (which interact with preambles).
834+
TEST(ParsedASTTest, HeaderGuardsImplIface) {
835+
std::string Interface = R"cpp(
836+
// error-ok: we assert on diagnostics explicitly
837+
template <class T> struct Traits {
838+
unsigned size();
839+
};
840+
#include "impl.h"
841+
)cpp";
842+
std::string Implementation = R"cpp(
843+
// error-ok: we assert on diagnostics explicitly
844+
#include "iface.h"
845+
template <class T> unsigned Traits<T>::size() {
846+
return sizeof(T);
847+
}
848+
)cpp";
849+
850+
TestTU TU;
851+
TU.ImplicitHeaderGuard = false; // We're testing include guard handling!
852+
TU.ExtraArgs.push_back("-xc++-header");
853+
854+
// Editing the interface file, which is include guarded (easy case).
855+
// We mostly get this right via PP if we don't recognize the include guard.
856+
TU.Filename = "iface.h";
857+
TU.Code = guard(Interface);
858+
TU.AdditionalFiles = {{"impl.h", Implementation}};
859+
auto AST = TU.build();
860+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
861+
EXPECT_TRUE(mainIsGuarded(AST));
862+
// Slightly harder: the `#pragma once` is part of the preamble, and we
863+
// need to transfer it to the main file's HeaderFileInfo.
864+
TU.Code = once(Interface);
865+
AST = TU.build();
866+
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty());
867+
EXPECT_TRUE(mainIsGuarded(AST));
868+
869+
// Editing the implementation file, which is not include guarded.
870+
TU.Filename = "impl.h";
871+
TU.Code = Implementation;
872+
TU.AdditionalFiles = {{"iface.h", guard(Interface)}};
873+
AST = TU.build();
874+
// The diagnostic is unfortunate in this case, but correct per our model.
875+
// Ultimately the include is skipped and the code is parsed correctly though.
876+
EXPECT_THAT(*AST.getDiagnostics(),
877+
ElementsAre(Diag("in included file: main file cannot be included "
878+
"recursively when building a preamble")));
879+
EXPECT_FALSE(mainIsGuarded(AST));
880+
// Interface is pragma once guarded, same thing.
881+
TU.AdditionalFiles = {{"iface.h", once(Interface)}};
882+
AST = TU.build();
883+
EXPECT_THAT(*AST.getDiagnostics(),
884+
ElementsAre(Diag("in included file: main file cannot be included "
885+
"recursively when building a preamble")));
886+
EXPECT_FALSE(mainIsGuarded(AST));
887+
}
888+
636889
} // namespace
637890
} // namespace clangd
638891
} // namespace clang

clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,7 @@ template <>
192192
const int f12() { return 0; }
193193
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: full function template specialization 'f12<int>' defined in a header file;
194194
// CHECK-FIXES: inline const int f12() { return 0; }
195+
196+
int main() {}
197+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'main' defined in a header file;
198+
// CHECK-FIXES: {{^}}int main() {

clang/cmake/caches/Fuchsia-stage2.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ set(LLVM_TOOLCHAIN_TOOLS
246246
llvm-dlltool
247247
llvm-dwarfdump
248248
llvm-dwp
249-
llvm-elfabi
249+
llvm-ifs
250250
llvm-gsymutil
251251
llvm-lib
252252
llvm-lipo

0 commit comments

Comments
 (0)