[Lexer] Don't merge macro args from different macro files
The lexer sets the end location of macro arguments incorrectly *if*,
while merging consecutive args to fit into a single SLocEntry, it finds
args which come from different macro files.
Fix the issue by using separate SLocEntries in this situation.
This fixes a code coverage crasher (rdar://problem/26181005). Because
the lexer reported end locations for certain macro args incorrectly, we
would generate bogus coverage mappings with negative line offsets.
Reviewed-by: akyrtzi
Differential Revision: http://reviews.llvm.org/D20401
llvm-svn: 270160
diff --git a/clang/unittests/Lex/LexerTest.cpp b/clang/unittests/Lex/LexerTest.cpp
index 0848f7b..2046018 100644
--- a/clang/unittests/Lex/LexerTest.cpp
+++ b/clang/unittests/Lex/LexerTest.cpp
@@ -58,8 +58,7 @@
Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
}
- std::vector<Token> CheckLex(StringRef Source,
- ArrayRef<tok::TokenKind> ExpectedTokens) {
+ std::vector<Token> Lex(StringRef Source) {
std::unique_ptr<llvm::MemoryBuffer> Buf =
llvm::MemoryBuffer::getMemBuffer(Source);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
@@ -82,6 +81,12 @@
toks.push_back(tok);
}
+ return toks;
+ }
+
+ std::vector<Token> CheckLex(StringRef Source,
+ ArrayRef<tok::TokenKind> ExpectedTokens) {
+ auto toks = Lex(Source);
EXPECT_EQ(ExpectedTokens.size(), toks.size());
for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) {
EXPECT_EQ(ExpectedTokens[i], toks[i].getKind());
@@ -358,4 +363,21 @@
EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts));
}
+TEST_F(LexerTest, DontMergeMacroArgsFromDifferentMacroFiles) {
+ std::vector<Token> toks =
+ Lex("#define helper1 0\n"
+ "void helper2(const char *, ...);\n"
+ "#define M1(a, ...) helper2(a, ##__VA_ARGS__)\n"
+ "#define M2(a, ...) M1(a, helper1, ##__VA_ARGS__)\n"
+ "void f1() { M2(\"a\", \"b\"); }");
+
+ // Check the file corresponding to the "helper1" macro arg in M2.
+ //
+ // The lexer used to report its size as 31, meaning that the end of the
+ // expansion would be on the *next line* (just past `M2("a", "b")`). Make
+ // sure that we get the correct end location (the comma after "helper1").
+ SourceLocation helper1ArgLoc = toks[20].getLocation();
+ EXPECT_EQ(SourceMgr.getFileIDSize(SourceMgr.getFileID(helper1ArgLoc)), 8U);
+}
+
} // anonymous namespace