From: Richard Smith Date: Thu, 23 Apr 2015 22:58:06 +0000 (+0000) Subject: [modules] Properly attribute macros to modules if they're in a file textually include... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b58f3164d3e1e1ce89dc6d7650a5b4f9638d7bea;p=clang [modules] Properly attribute macros to modules if they're in a file textually included into a file in the module. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@235661 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 94c4e24e96..635428e86a 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -1669,9 +1669,14 @@ private: void HandleMicrosoftImportDirective(Token &Tok); // Module inclusion testing. - /// \brief Find the module for the source or header file that \p FilenameLoc - /// points to. - Module *getModuleForLocation(SourceLocation FilenameLoc); + /// \brief Find the module that owns the source or header file that + /// \p Loc points to. If the location is in a file that was included + /// into a module, or is outside any module, returns nullptr. + Module *getModuleForLocation(SourceLocation Loc); + + /// \brief Find the module that contains the specified location, either + /// directly or indirectly. + Module *getModuleContainingLocation(SourceLocation Loc); // Macro handling. void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterTopLevelIfndef); diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index 4f3efa526c..9084915dcd 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -577,16 +577,16 @@ void Preprocessor::PTHSkipExcludedConditionalBlock() { } } -Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) { +Module *Preprocessor::getModuleForLocation(SourceLocation Loc) { ModuleMap &ModMap = HeaderInfo.getModuleMap(); - if (SourceMgr.isInMainFile(FilenameLoc)) { + if (SourceMgr.isInMainFile(Loc)) { if (Module *CurMod = getCurrentModule()) return CurMod; // Compiling a module. return HeaderInfo.getModuleMap().SourceModule; // Compiling a source. } // Try to determine the module of the include directive. // FIXME: Look into directly passing the FileEntry from LookupFile instead. - FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(FilenameLoc)); + FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc)); if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) { // The include comes from a file. return ModMap.findModuleForHeader(EntryOfIncl).getModule(); @@ -597,6 +597,11 @@ Module *Preprocessor::getModuleForLocation(SourceLocation FilenameLoc) { } } +Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) { + return HeaderInfo.getModuleMap().inferModuleFromLocation( + FullSourceLoc(Loc, SourceMgr)); +} + const FileEntry *Preprocessor::LookupFile( SourceLocation FilenameLoc, StringRef Filename, diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp index 33f5ff07f0..8a74a95ab3 100644 --- a/lib/Lex/PPLexerChange.cpp +++ b/lib/Lex/PPLexerChange.cpp @@ -633,7 +633,7 @@ void Preprocessor::LeaveSubmodule() { for (auto *MD = Macro.second.getLatest(); MD != State.getLatest(); MD = MD->getPrevious()) { // Skip macros defined in other submodules we #included along the way. - Module *Mod = getModuleForLocation(MD->getLocation()); + Module *Mod = getModuleContainingLocation(MD->getLocation()); if (Mod != Info.M) continue; diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index 54f72f6462..174718f438 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -60,12 +60,12 @@ void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ // Accumulate any overridden imported macros. if (!MD->isImported() && getCurrentModule()) { - Module *OwningMod = getModuleForLocation(MD->getLocation()); + Module *OwningMod = getModuleContainingLocation(MD->getLocation()); if (!OwningMod) return; for (auto *PrevMD = OldMD; PrevMD; PrevMD = PrevMD->getPrevious()) { - Module *DirectiveMod = getModuleForLocation(PrevMD->getLocation()); + Module *DirectiveMod = getModuleContainingLocation(PrevMD->getLocation()); if (ModuleMacro *PrevMM = PrevMD->getOwningModuleMacro()) StoredMD.addOverriddenMacro(*this, PrevMM); else if (ModuleMacro *PrevMM = getModuleMacro(DirectiveMod, II)) diff --git a/test/Modules/Inputs/macros-indirect.h b/test/Modules/Inputs/macros-indirect.h new file mode 100644 index 0000000000..c90300e464 --- /dev/null +++ b/test/Modules/Inputs/macros-indirect.h @@ -0,0 +1 @@ +#define INDIRECTLY_IN_MACROS 1 diff --git a/test/Modules/Inputs/macros.h b/test/Modules/Inputs/macros.h index 27f43c0626..a0ae7a3158 100644 --- a/test/Modules/Inputs/macros.h +++ b/test/Modules/Inputs/macros.h @@ -17,3 +17,4 @@ int (INTEGER); extern int __MODULE__; #endif +#include "macros-indirect.h" diff --git a/test/Modules/macros.c b/test/Modules/macros.c index 92ea88a480..3ca53005cf 100644 --- a/test/Modules/macros.c +++ b/test/Modules/macros.c @@ -28,6 +28,10 @@ # error MODULE macro should not be visible #endif +#ifndef INDIRECTLY_IN_MACROS +# error INDIRECTLY_IN_MACROS should be visible +#endif + // CHECK-PREPROCESSED: double d double d; DOUBLE *dp = &d;