diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2012-03-01 04:18:49 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2012-03-01 04:18:49 +0000 |
commit | 7abe1666f225b6d1a11aa7ed19d9a0dcc49391cb (patch) | |
tree | 1f00308f151460e44b2fc9af6abec81749d045c3 | |
parent | 6454a02e7bbda180ef3867b6ae3c1aee327a34a7 (diff) |
Implements support for #pragma include_alias in ms compatibility mode. Fixes PR10705.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151800 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticLexKinds.td | 9 | ||||
-rw-r--r-- | include/clang/Lex/HeaderSearch.h | 22 | ||||
-rw-r--r-- | include/clang/Lex/Preprocessor.h | 1 | ||||
-rw-r--r-- | lib/Lex/PPDirectives.cpp | 3 | ||||
-rw-r--r-- | lib/Lex/Pragma.cpp | 105 | ||||
-rw-r--r-- | test/Preprocessor/pragma_microsoft.c | 17 |
6 files changed, 157 insertions, 0 deletions
diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td index 46e0bebb3e..d7ed55a81f 100644 --- a/include/clang/Basic/DiagnosticLexKinds.td +++ b/include/clang/Basic/DiagnosticLexKinds.td @@ -292,6 +292,15 @@ def warn_has_warning_invalid_option : ExtWarn<"__has_warning expected option name (e.g. \"-Wundef\")">, InGroup<MalformedWarningCheck>; +def warn_pragma_include_alias_mismatch : + ExtWarn<"pragma include_alias requires matching include directives " + "(e.g include_alias(\"foo.h\", \"bar.h\") or " + "include_alias(<foo.h>, <bar.h>))">, + InGroup<UnknownPragmas>; +def warn_pragma_include_alias_expected : + ExtWarn<"pragma include_alias expected '%0'">, + InGroup<UnknownPragmas>; + def err__Pragma_malformed : Error< "_Pragma takes a parenthesized string literal">; def err_pragma_comment_malformed : Error< diff --git a/include/clang/Lex/HeaderSearch.h b/include/clang/Lex/HeaderSearch.h index 84bb37da3a..bbd7146cf4 100644 --- a/include/clang/Lex/HeaderSearch.h +++ b/include/clang/Lex/HeaderSearch.h @@ -154,6 +154,9 @@ class HeaderSearch { llvm::StringMap<const DirectoryEntry *, llvm::BumpPtrAllocator> FrameworkMap; + llvm::StringMap<std::pair<StringRef, bool>, llvm::BumpPtrAllocator> + IncludeAliasMap; + /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing /// headermaps. This vector owns the headermap. std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps; @@ -217,6 +220,25 @@ public: SystemDirIdx++; } + /// AddHeaderMapping -- Map the source include name to the dest include name + void AddHeaderMapping(const StringRef& Source, const StringRef& Dest, + bool IsAngled) { + IncludeAliasMap[Source] = std::make_pair(Dest, IsAngled); + } + + StringRef MapHeader(const StringRef& Source, bool isAngled) { + // Do any filename replacements before anything else + llvm::StringMap<std::pair<StringRef,bool>>::const_iterator iter = + IncludeAliasMap.find(Source); + if (iter != IncludeAliasMap.end()) { + // If the angling matches, then we've found a replacement + if (iter->second.second == isAngled) { + return iter->second.first; + } + } + return Source; + } + /// \brief Set the path to the module cache. void setModuleCachePath(StringRef CachePath) { ModuleCachePath = CachePath; diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index feed6a8bfa..9ee9e82529 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -1262,6 +1262,7 @@ public: void HandlePragmaMessage(Token &MessageTok); void HandlePragmaPushMacro(Token &Tok); void HandlePragmaPopMacro(Token &Tok); + void HandlePragmaIncludeAlias(Token &Tok); IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok); // Return true and store the first token only if any CommentHandler diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index 0364e4279a..4839dfa4a1 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -1304,6 +1304,9 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, PragmaARCCFCodeAuditedLoc = SourceLocation(); } + // Map the filename + Filename = HeaderInfo.MapHeader(Filename, isAngled); + // Search include directories. const DirectoryLookup *CurDir; SmallString<1024> SearchPath; diff --git a/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index 503fffe53d..7a6ab7dd72 100644 --- a/lib/Lex/Pragma.cpp +++ b/lib/Lex/Pragma.cpp @@ -663,6 +663,101 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { } } +void Preprocessor::HandlePragmaIncludeAlias(Token& Tok) { + // We will either get a quoted filename or a bracketed filename, and we + // have to track which we got. The first filename is the source name, + // and the second name is the mapped filename. If the first is quoted, + // the second must be as well (cannot mix and match quotes and brackets). + SourceLocation Loc = Tok.getLocation(); + + // Get the open paren + Lex(Tok); + if (Tok.isNot(tok::l_paren)) { + Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; + return; + } + + // We expect either a quoted string literal, or a bracketed name + Token SourceFilenameTok; + CurPPLexer->LexIncludeFilename(SourceFilenameTok); + if (SourceFilenameTok.is(tok::eod)) { + // The diagnostic has already been handled + return; + } + + StringRef SourceFileName; + SmallString<128> FileNameBuffer; + if (SourceFilenameTok.is(tok::string_literal) || + SourceFilenameTok.is(tok::angle_string_literal)) { + SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); + } else if (SourceFilenameTok.is(tok::less)) { + // This could be a path instead of just a name + FileNameBuffer.push_back('<'); + SourceLocation End; + if (ConcatenateIncludeName(FileNameBuffer, End)) + return; // Diagnostic already emitted + SourceFileName = FileNameBuffer.str(); + } else { + Diag(Tok, diag::warn_pragma_include_alias_expected) << "include filename"; + return; + } + FileNameBuffer.clear(); + + // Now we expect a comma, followed by another include name + Lex(Tok); + if (Tok.isNot(tok::comma)) { + Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; + return; + } + + Token ReplaceFilenameTok; + CurPPLexer->LexIncludeFilename(ReplaceFilenameTok); + if (ReplaceFilenameTok.is(tok::eod)) { + // The diagnostic has already been handled + return; + } + + StringRef ReplaceFileName; + if (ReplaceFilenameTok.is(tok::string_literal) || + ReplaceFilenameTok.is(tok::angle_string_literal)) { + ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); + } else if (ReplaceFilenameTok.is(tok::less)) { + // This could be a path instead of just a name + FileNameBuffer.push_back('<'); + SourceLocation End; + if (ConcatenateIncludeName(FileNameBuffer, End)) + return; // Diagnostic already emitted + ReplaceFileName = FileNameBuffer.str(); + } else { + Diag(Tok, diag::warn_pragma_include_alias_expected) << "include filename"; + return; + } + + // Finally, we expect the closing paren + Lex(Tok); + if (Tok.isNot(tok::r_paren)) { + Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; + return; + } + + // Now that we have the source and target filenames, we need to make sure + // they're both of the same type (angled vs non-angled) + bool SourceIsAngled = + GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), + SourceFileName); + bool ReplaceIsAngled = + GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), + ReplaceFileName); + if (SourceIsAngled != ReplaceIsAngled) { + Diag(Loc, diag::warn_pragma_include_alias_mismatch); + return; + } + + // Now we can let the include handler know about this mapping + getHeaderSearchInfo().AddHeaderMapping(SourceFileName, ReplaceFileName, + SourceIsAngled); +} + /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. /// If 'Namespace' is non-null, then it is a token required to exist on the /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". @@ -943,6 +1038,15 @@ struct PragmaCommentHandler : public PragmaHandler { } }; +/// PragmaIncludeAliasHandler - "#pragma include_alias("...")". +struct PragmaIncludeAliasHandler : public PragmaHandler { + PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} + virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, + Token &IncludeAliasTok) { + PP.HandlePragmaIncludeAlias(IncludeAliasTok); + } +}; + /// PragmaMessageHandler - "#pragma message("...")". struct PragmaMessageHandler : public PragmaHandler { PragmaMessageHandler() : PragmaHandler("message") {} @@ -1095,5 +1199,6 @@ void Preprocessor::RegisterBuiltinPragmas() { // MS extensions. if (Features.MicrosoftExt) { AddPragmaHandler(new PragmaCommentHandler()); + AddPragmaHandler(new PragmaIncludeAliasHandler()); } } diff --git a/test/Preprocessor/pragma_microsoft.c b/test/Preprocessor/pragma_microsoft.c index b68d6e363e..88925ffdb2 100644 --- a/test/Preprocessor/pragma_microsoft.c +++ b/test/Preprocessor/pragma_microsoft.c @@ -38,3 +38,20 @@ void f() // this warning should go away. MACRO_WITH__PRAGMA // expected-warning {{expression result unused}} } + + +// This should include macro_arg_directive even though the include +// is looking for test.h This allows us to assign to "n" +#pragma include_alias("test.h", "macro_arg_directive.h" ) +#include "test.h" +void test( void ) { + n = 12; +} + +#pragma include_alias("foo.h", <bar.h>) // expected-warning {{pragma include_alias requires matching include directives (e.g include_alias("foo.h", "bar.h") or include_alias(<foo.h>, <bar.h>))}} +#pragma include_alias("test.h") // expected-warning {{pragma include_alias expected ','}} + +// Make sure that the names match exactly for a replacement, including path information. If +// this were to fail, we would get a file not found error +#pragma include_alias(".\pp-record.h", "does_not_exist.h") +#include "pp-record.h" |