Skip to content

Commit dff4711

Browse files
zmodemhyp
authored andcommitted
Revert "[clang][pp] adds '#pragma include_instead'"
> `#pragma clang include_instead(<header>)` is a pragma that can be used > by system headers (and only system headers) to indicate to a tool that > the file containing said pragma is an implementation-detail header and > should not be directly included by user code. > > The library alternative is very messy code that can be seen in the first > diff of D106124, and we'd rather avoid that with something more > universal. > > This patch takes the first step by warning a user when they include a > detail header in their code, and suggests alternative headers that the > user should include instead. Future work will involve adding a fixit to > automate the process, as well as cleaning up modules diagnostics to not > suggest said detail headers. Other tools, such as clangd can also take > advantage of this pragma to add the correct user headers. > > Differential Revision: https://reviews.llvm.org/D106394 This caused compiler crashes in Chromium builds involving PCH and an include directive with macro expansion, when Token::getLiteralData() returned null. See the code review for details. This reverts commit e8a64e5. (cherry picked from commit 973de71)
1 parent 406b96e commit dff4711

20 files changed

+23
-225
lines changed

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,6 @@ def pp_pragma_once_in_main_file : Warning<"#pragma once in main file">,
300300
def pp_pragma_sysheader_in_main_file : Warning<
301301
"#pragma system_header ignored in main file">,
302302
InGroup<DiagGroup<"pragma-system-header-outside-header">>;
303-
304-
def err_pragma_include_instead_not_sysheader : Error<
305-
"'#pragma clang include_instead' cannot be used outside of system headers">;
306-
def err_pragma_include_instead_system_reserved : Error<
307-
"header '%0' is an implementation detail; #include %select{'%2'|either '%2' or '%3'|one of %2}1 instead">;
308-
309303
def pp_poisoning_existing_macro : Warning<"poisoning existing macro">;
310304
def pp_out_of_date_dependency : Warning<
311305
"current file is older than dependency %0">;

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
#include "clang/Lex/ModuleMap.h"
2121
#include "llvm/ADT/ArrayRef.h"
2222
#include "llvm/ADT/DenseMap.h"
23-
#include "llvm/ADT/SetVector.h"
24-
#include "llvm/ADT/SmallSet.h"
25-
#include "llvm/ADT/SmallString.h"
2623
#include "llvm/ADT/StringMap.h"
27-
#include "llvm/ADT/StringRef.h"
2824
#include "llvm/ADT/StringSet.h"
25+
#include "llvm/ADT/StringRef.h"
2926
#include "llvm/Support/Allocator.h"
3027
#include <cassert>
3128
#include <cstddef>
@@ -113,14 +110,6 @@ struct HeaderFileInfo {
113110
/// of the framework.
114111
StringRef Framework;
115112

116-
/// List of aliases that this header is known as.
117-
/// Most headers should only have at most one alias, but a handful
118-
/// have two.
119-
llvm::SetVector<llvm::SmallString<32>,
120-
llvm::SmallVector<llvm::SmallString<32>, 2>,
121-
llvm::SmallSet<llvm::SmallString<32>, 2>>
122-
Aliases;
123-
124113
HeaderFileInfo()
125114
: isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
126115
External(false), isModuleHeader(false), isCompilingModuleHeader(false),
@@ -464,10 +453,6 @@ class HeaderSearch {
464453
getFileInfo(File).DirInfo = SrcMgr::C_System;
465454
}
466455

467-
void AddFileAlias(const FileEntry *File, StringRef Alias) {
468-
getFileInfo(File).Aliases.insert(Alias);
469-
}
470-
471456
/// Mark the specified file as part of a module.
472457
void MarkFileModuleHeader(const FileEntry *FE,
473458
ModuleMap::ModuleHeaderRole Role,

clang/include/clang/Lex/Preprocessor.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,8 +1953,7 @@ class Preprocessor {
19531953
/// This either returns the EOF token and returns true, or
19541954
/// pops a level off the include stack and returns false, at which point the
19551955
/// client should call lex again.
1956-
bool HandleEndOfFile(Token &Result, SourceLocation Loc,
1957-
bool isEndOfMacro = false);
1956+
bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
19581957

19591958
/// Callback invoked when the current TokenLexer hits the end of its
19601959
/// token stream.
@@ -2364,14 +2363,12 @@ class Preprocessor {
23642363

23652364
// Pragmas.
23662365
void HandlePragmaDirective(PragmaIntroducer Introducer);
2367-
void ResolvePragmaIncludeInstead(SourceLocation Location) const;
23682366

23692367
public:
23702368
void HandlePragmaOnce(Token &OnceTok);
23712369
void HandlePragmaMark(Token &MarkTok);
23722370
void HandlePragmaPoison();
23732371
void HandlePragmaSystemHeader(Token &SysHeaderTok);
2374-
void HandlePragmaIncludeInstead(Token &Tok);
23752372
void HandlePragmaDependency(Token &DependencyTok);
23762373
void HandlePragmaPushMacro(Token &Tok);
23772374
void HandlePragmaPopMacro(Token &Tok);

clang/include/clang/Lex/PreprocessorLexer.h

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
#ifndef LLVM_CLANG_LEX_PREPROCESSORLEXER_H
1515
#define LLVM_CLANG_LEX_PREPROCESSORLEXER_H
1616

17-
#include "clang/Basic/SourceLocation.h"
18-
#include "clang/Lex/HeaderSearch.h"
1917
#include "clang/Lex/MultipleIncludeOpt.h"
2018
#include "clang/Lex/Token.h"
19+
#include "clang/Basic/SourceLocation.h"
2120
#include "llvm/ADT/ArrayRef.h"
2221
#include "llvm/ADT/SmallVector.h"
23-
#include "llvm/ADT/StringMap.h"
2422
#include <cassert>
2523

2624
namespace clang {
@@ -76,13 +74,6 @@ class PreprocessorLexer {
7674
/// we are currently in.
7775
SmallVector<PPConditionalInfo, 4> ConditionalStack;
7876

79-
struct IncludeInfo {
80-
const FileEntry *File;
81-
SourceLocation Location;
82-
};
83-
// A complete history of all the files included by the current file.
84-
llvm::StringMap<IncludeInfo> IncludeHistory;
85-
8677
PreprocessorLexer() : FID() {}
8778
PreprocessorLexer(Preprocessor *pp, FileID fid);
8879
virtual ~PreprocessorLexer() = default;
@@ -184,15 +175,6 @@ class PreprocessorLexer {
184175
ConditionalStack.clear();
185176
ConditionalStack.append(CL.begin(), CL.end());
186177
}
187-
188-
void addInclude(StringRef Filename, const FileEntry &File,
189-
SourceLocation Location) {
190-
IncludeHistory.insert({Filename, {&File, Location}});
191-
}
192-
193-
const llvm::StringMap<IncludeInfo> &getIncludeHistory() const {
194-
return IncludeHistory;
195-
}
196178
};
197179

198180
} // namespace clang

clang/lib/Lex/Lexer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,11 +2840,11 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
28402840
ConditionalStack.pop_back();
28412841
}
28422842

2843-
SourceLocation EndLoc = getSourceLocation(BufferEnd);
28442843
// C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
28452844
// a pedwarn.
28462845
if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) {
28472846
DiagnosticsEngine &Diags = PP->getDiagnostics();
2847+
SourceLocation EndLoc = getSourceLocation(BufferEnd);
28482848
unsigned DiagID;
28492849

28502850
if (LangOpts.CPlusPlus11) {
@@ -2867,7 +2867,7 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
28672867
BufferPtr = CurPtr;
28682868

28692869
// Finally, let the preprocessor handle this.
2870-
return PP->HandleEndOfFile(Result, EndLoc, isPragmaLexer());
2870+
return PP->HandleEndOfFile(Result, isPragmaLexer());
28712871
}
28722872

28732873
/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from

clang/lib/Lex/PPDirectives.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,12 +2020,6 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
20202020
IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
20212021
LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
20222022

2023-
// Record the header's filename for later use.
2024-
if (File)
2025-
CurLexer->addInclude(
2026-
{FilenameTok.getLiteralData(), FilenameTok.getLength()},
2027-
File->getFileEntry(), FilenameLoc);
2028-
20292023
if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
20302024
if (File && isPCHThroughHeader(&File->getFileEntry()))
20312025
SkippingUntilPCHThroughHeader = false;

clang/lib/Lex/PPLexerChange.cpp

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

1414
#include "clang/Basic/FileManager.h"
15-
#include "clang/Basic/SourceLocation.h"
1615
#include "clang/Basic/SourceManager.h"
1716
#include "clang/Lex/HeaderSearch.h"
1817
#include "clang/Lex/LexDiagnostic.h"
@@ -23,7 +22,6 @@
2322
#include "llvm/Support/FileSystem.h"
2423
#include "llvm/Support/MemoryBufferRef.h"
2524
#include "llvm/Support/Path.h"
26-
2725
using namespace clang;
2826

2927
//===----------------------------------------------------------------------===//
@@ -301,46 +299,10 @@ void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
301299
}
302300
}
303301

304-
void Preprocessor::ResolvePragmaIncludeInstead(
305-
const SourceLocation Location) const {
306-
assert(Location.isValid());
307-
if (CurLexer == nullptr)
308-
return;
309-
310-
if (SourceMgr.isInSystemHeader(Location))
311-
return;
312-
313-
for (const auto &Include : CurLexer->getIncludeHistory()) {
314-
StringRef Filename = Include.getKey();
315-
const PreprocessorLexer::IncludeInfo &Info = Include.getValue();
316-
ArrayRef<SmallString<32>> Aliases =
317-
HeaderInfo.getFileInfo(Info.File).Aliases.getArrayRef();
318-
319-
if (Aliases.empty())
320-
continue;
321-
322-
switch (Aliases.size()) {
323-
case 1:
324-
Diag(Info.Location, diag::err_pragma_include_instead_system_reserved)
325-
<< Filename << 0 << Aliases[0];
326-
continue;
327-
case 2:
328-
Diag(Info.Location, diag::err_pragma_include_instead_system_reserved)
329-
<< Filename << 1 << Aliases[0] << Aliases[1];
330-
continue;
331-
default: {
332-
Diag(Info.Location, diag::err_pragma_include_instead_system_reserved)
333-
<< Filename << 2 << ("{'" + llvm::join(Aliases, "', '") + "'}");
334-
}
335-
}
336-
}
337-
}
338-
339302
/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
340303
/// the current file. This either returns the EOF token or pops a level off
341304
/// the include stack and keeps going.
342-
bool Preprocessor::HandleEndOfFile(Token &Result, SourceLocation EndLoc,
343-
bool isEndOfMacro) {
305+
bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
344306
assert(!CurTokenLexer &&
345307
"Ending a file when currently in a macro!");
346308

@@ -410,9 +372,6 @@ bool Preprocessor::HandleEndOfFile(Token &Result, SourceLocation EndLoc,
410372
}
411373
}
412374

413-
if (EndLoc.isValid())
414-
ResolvePragmaIncludeInstead(EndLoc);
415-
416375
// Complain about reaching a true EOF within arc_cf_code_audited.
417376
// We don't want to complain about reaching the end of a macro
418377
// instantiation or a _Pragma.
@@ -601,7 +560,7 @@ bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
601560
TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
602561

603562
// Handle this like a #include file being popped off the stack.
604-
return HandleEndOfFile(Result, {}, true);
563+
return HandleEndOfFile(Result, true);
605564
}
606565

607566
/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the

0 commit comments

Comments
 (0)