зеркало из https://github.com/microsoft/clang-1.git
Implement checking for macro definitions that occur on the command
line when using a PCH that were not provided when building the PCH file. If those names were used as identifiers somewhere in the PCH file, reject the PCH file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70321 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
02fc75169a
Коммит
92b059ea94
|
@ -123,5 +123,7 @@ def warn_cmdline_missing_macro_defs : Warning<
|
||||||
"macro definitions used to build the precompiled header are missing">;
|
"macro definitions used to build the precompiled header are missing">;
|
||||||
def note_using_macro_def_from_pch : Note<
|
def note_using_macro_def_from_pch : Note<
|
||||||
"using this macro definition from precompiled header">;
|
"using this macro definition from precompiled header">;
|
||||||
|
def warn_macro_name_used_in_pch : Warning<
|
||||||
|
"definition of macro %0 conflicts with an identifier used in the "
|
||||||
|
"precompiled header">;
|
||||||
}
|
}
|
||||||
|
|
|
@ -280,6 +280,16 @@ private:
|
||||||
/// Objective-C protocols.
|
/// Objective-C protocols.
|
||||||
llvm::SmallVector<Decl *, 16> InterestingDecls;
|
llvm::SmallVector<Decl *, 16> InterestingDecls;
|
||||||
|
|
||||||
|
/// \brief The file ID for the predefines buffer in the PCH file.
|
||||||
|
FileID PCHPredefinesBufferID;
|
||||||
|
|
||||||
|
/// \brief Pointer to the beginning of the predefines buffer in the
|
||||||
|
/// PCH file.
|
||||||
|
const char *PCHPredefines;
|
||||||
|
|
||||||
|
/// \brief Length of the predefines buffer in the PCH file.
|
||||||
|
unsigned PCHPredefinesLen;
|
||||||
|
|
||||||
/// \brief Suggested contents of the predefines buffer, after this
|
/// \brief Suggested contents of the predefines buffer, after this
|
||||||
/// PCH file has been processed.
|
/// PCH file has been processed.
|
||||||
///
|
///
|
||||||
|
|
|
@ -507,9 +507,17 @@ bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
|
||||||
std::string MacroName = Extra.substr(StartOfMacroName,
|
std::string MacroName = Extra.substr(StartOfMacroName,
|
||||||
EndOfMacroName - StartOfMacroName);
|
EndOfMacroName - StartOfMacroName);
|
||||||
|
|
||||||
// FIXME: Perform this check!
|
// Check whether this name was used somewhere in the PCH file. If
|
||||||
fprintf(stderr, "FIXME: check whether '%s' was used in the PCH file\n",
|
// so, defining it as a macro could change behavior, so we reject
|
||||||
MacroName.c_str());
|
// the PCH file.
|
||||||
|
if (IdentifierInfo *II = get(MacroName.c_str(),
|
||||||
|
MacroName.c_str() + MacroName.size())) {
|
||||||
|
Diag(diag::warn_macro_name_used_in_pch)
|
||||||
|
<< II;
|
||||||
|
Diag(diag::note_ignoring_pch)
|
||||||
|
<< FileName;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Add this definition to the suggested predefines buffer.
|
// Add this definition to the suggested predefines buffer.
|
||||||
SuggestedPredefines += Extra;
|
SuggestedPredefines += Extra;
|
||||||
|
@ -818,9 +826,11 @@ PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) {
|
||||||
Name);
|
Name);
|
||||||
FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
|
FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
|
||||||
|
|
||||||
if (strcmp(Name, "<built-in>") == 0
|
if (strcmp(Name, "<built-in>") == 0) {
|
||||||
&& CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
|
PCHPredefinesBufferID = BufferID;
|
||||||
return IgnorePCH;
|
PCHPredefines = BlobStart;
|
||||||
|
PCHPredefinesLen = BlobLen - 1;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1288,6 +1298,11 @@ PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
|
||||||
if (Context)
|
if (Context)
|
||||||
ReadDeclRecord(DeclOffsets[0], 0);
|
ReadDeclRecord(DeclOffsets[0], 0);
|
||||||
|
|
||||||
|
// Check the predefines buffer.
|
||||||
|
if (CheckPredefinesBuffer(PCHPredefines, PCHPredefinesLen,
|
||||||
|
PCHPredefinesBufferID))
|
||||||
|
return IgnorePCH;
|
||||||
|
|
||||||
// Initialization of builtins and library builtins occurs before the
|
// Initialization of builtins and library builtins occurs before the
|
||||||
// PCH file is read, so there may be some identifiers that were
|
// PCH file is read, so there may be some identifiers that were
|
||||||
// loaded into the IdentifierTable before we intercepted the
|
// loaded into the IdentifierTable before we intercepted the
|
||||||
|
|
|
@ -1378,11 +1378,21 @@ void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
|
||||||
|
|
||||||
// Create and write out the blob that contains the identifier
|
// Create and write out the blob that contains the identifier
|
||||||
// strings.
|
// strings.
|
||||||
IdentifierOffsets.resize(IdentifierIDs.size());
|
|
||||||
{
|
{
|
||||||
OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
|
OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
|
||||||
|
|
||||||
|
// Look for any identifiers that were named while processing the
|
||||||
|
// headers, but are otherwise not needed. We add these to the hash
|
||||||
|
// table to enable checking of the predefines buffer in the case
|
||||||
|
// where the user adds new macro definitions when building the PCH
|
||||||
|
// file.
|
||||||
|
for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
|
||||||
|
IDEnd = PP.getIdentifierTable().end();
|
||||||
|
ID != IDEnd; ++ID)
|
||||||
|
getIdentifierRef(ID->second);
|
||||||
|
|
||||||
// Create the on-disk hash table representation.
|
// Create the on-disk hash table representation.
|
||||||
|
IdentifierOffsets.resize(IdentifierIDs.size());
|
||||||
for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
|
for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
|
||||||
ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
|
ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
|
||||||
ID != IDEnd; ++ID) {
|
ID != IDEnd; ++ID) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
// Test with pch.
|
// Test with pch.
|
||||||
// RUN: clang-cc -emit-pch -DFOO -o %t %S/variables.h &&
|
// RUN: clang-cc -emit-pch -DFOO -o %t %S/variables.h &&
|
||||||
// RUN: clang-cc -DBAR=int -include-pch %t -fsyntax-only -pedantic %s
|
// RUN: clang-cc -DBAR=int -include-pch %t -fsyntax-only -pedantic %s &&
|
||||||
|
// RUN: clang-cc -DFOO -DBAR=int -include-pch %t -Werror %s &&
|
||||||
|
// RUN: not clang-cc -DFOO -DBAR=int -DX=5 -include-pch %t -Werror %s
|
||||||
|
|
||||||
BAR bar = 17;
|
BAR bar = 17;
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче