[PATCH] D98483: [clang] Mark re-injected tokens appropriately during pragma handling
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 12 09:14:33 PST 2021
kadircet updated this revision to Diff 330266.
kadircet added a comment.
- Introduce a helper to mark tokens as reinjected
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D98483/new/
https://reviews.llvm.org/D98483
Files:
clang/lib/Parse/ParsePragma.cpp
clang/unittests/Tooling/Syntax/TokensTest.cpp
Index: clang/unittests/Tooling/Syntax/TokensTest.cpp
===================================================================
--- clang/unittests/Tooling/Syntax/TokensTest.cpp
+++ clang/unittests/Tooling/Syntax/TokensTest.cpp
@@ -1037,4 +1037,13 @@
IsEmpty());
}
+TEST_F(TokenCollectorTest, Pragmas) {
+ // Tokens coming from concatenations.
+ recordTokens(R"cpp(
+ void foo() {
+ #pragma unroll 4
+ for(int i=0;i<4;++i);
+ }
+ )cpp");
+}
} // namespace
Index: clang/lib/Parse/ParsePragma.cpp
===================================================================
--- clang/lib/Parse/ParsePragma.cpp
+++ clang/lib/Parse/ParsePragma.cpp
@@ -14,11 +14,13 @@
#include "clang/Basic/PragmaKinds.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/Token.h"
#include "clang/Parse/LoopHint.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/RAIIObjectsForParser.h"
#include "clang/Sema/Scope.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringSwitch.h"
using namespace clang;
@@ -292,6 +294,10 @@
Token &FirstToken) override;
};
+void markAsReinjectedForRelexing(llvm::MutableArrayRef<clang::Token> Toks) {
+ for (auto &T : Toks)
+ T.setFlag(clang::Token::IsReinjected);
+}
} // end namespace
void Parser::initializePragmaHandlers() {
@@ -2621,6 +2627,7 @@
TokenVector.push_back(EoF);
// We must allocate this array with new because EnterTokenStream is going to
// delete it later.
+ markAsReinjectedForRelexing(TokenVector);
auto TokenArray = std::make_unique<Token[]>(TokenVector.size());
std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
auto Value = new (PP.getPreprocessorAllocator())
@@ -3178,6 +3185,7 @@
EOFTok.setLocation(Tok.getLocation());
ValueList.push_back(EOFTok); // Terminates expression for parsing.
+ markAsReinjectedForRelexing(ValueList);
Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Info.PragmaName = PragmaName;
@@ -3634,6 +3642,7 @@
EOFTok.setLocation(EndLoc);
AttributeTokens.push_back(EOFTok);
+ markAsReinjectedForRelexing(AttributeTokens);
Info->Tokens =
llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98483.330266.patch
Type: text/x-patch
Size: 2346 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210312/f3d316e0/attachment.bin>
More information about the cfe-commits
mailing list