Commit d9bc87ec authored by Lukasz Anforowicz's avatar Lukasz Anforowicz Committed by Commit Bot

Avoid rewriting fields in third-party code.

Bug: 2181165
Change-Id: Id1c945d28c3f5626cdd1b4d58b34cf61fc795bff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2202097Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarBartek Nowierski <bartekn@chromium.org>
Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org>
Cr-Commit-Position: refs/heads/master@{#771293}
parent f1735ff5
......@@ -84,6 +84,41 @@ AST_MATCHER(clang::TagDecl, isNotFreeStandingTagDecl) {
return !tag_decl->isFreeStanding();
}
llvm::StringRef GetFilePath(const clang::SourceManager& source_manager,
const clang::FieldDecl& field_decl) {
clang::SourceLocation loc = field_decl.getSourceRange().getBegin();
if (loc.isInvalid() || !loc.isFileID())
return llvm::StringRef();
clang::FileID file_id = source_manager.getDecomposedLoc(loc).first;
const clang::FileEntry* file_entry =
source_manager.getFileEntryForID(file_id);
if (!file_entry)
return llvm::StringRef();
return file_entry->getName();
}
AST_MATCHER(clang::FieldDecl, isInThirdPartyLocation) {
llvm::StringRef file_path =
GetFilePath(Finder->getASTContext().getSourceManager(), Node);
// Blink is part of the Chromium git repo, even though it contains
// "third_party" in its path.
if (file_path.contains("third_party/blink/"))
return false;
// V8 needs to be considered "third party", even though its paths do not
// contain the "third_party" substring. In particular, the rewriter should
// not append |.get()| to references to |v8::RegisterState::pc|, because
// //v8/include/v8.h will *not* get rewritten.
if (file_path.contains("v8/include/"))
return true;
// Otherwise, just check if the paths contains the "third_party" substring.
return file_path.contains("third_party");
}
AST_MATCHER(clang::ClassTemplateSpecializationDecl, isImplicitSpecialization) {
return !Node.isExplicitSpecialization();
}
......@@ -260,7 +295,9 @@ int main(int argc, const char* argv[]) {
// - fields of lambda-supporting classes
auto field_decl_matcher =
fieldDecl(allOf(hasType(supported_pointer_types_matcher),
hasUniqueTypeLoc(), unless(implicit_field_decl_matcher)))
hasUniqueTypeLoc(),
unless(anyOf(isInThirdPartyLocation(),
implicit_field_decl_matcher))))
.bind("fieldDecl");
FieldDeclRewriter field_decl_rewriter(&replacements_printer);
match_finder.addMatcher(field_decl_matcher, &field_decl_rewriter);
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This test file simulates source files similar to //base/third_party. Such
// files are part of Chromium repo (according to |git|) and therefore would be
// rewritten by the |apply_edits.py| tool. OTOH, we don't want to rewrite such
// files:
// 1. We want to minimize the delta between the upstream third-party repo and
// the Chromium copy
// 2. Such files very often limit themselves to C-only and CheckedPtr requires
// C++11. (OTOH, if needed this item might be more directly addressable by
// looking at |extern "C"| context declarations, or by looking at the
// language of the source code - as determined by
// |clang_frontend_input_file.getKind().getLanguage()|).
class SomeClass;
struct MyStruct {
// No rewrite expected, because the path of this source file contains
// "third_party" substring.
SomeClass* ptr_field;
};
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This test file simulates source files similar to //base/third_party. Such
// files are part of Chromium repo (according to |git|) and therefore would be
// rewritten by the |apply_edits.py| tool. OTOH, we don't want to rewrite such
// files:
// 1. We want to minimize the delta between the upstream third-party repo and
// the Chromium copy
// 2. Such files very often limit themselves to C-only and CheckedPtr requires
// C++11. (OTOH, if needed this item might be more directly addressable by
// looking at |extern "C"| context declarations, or by looking at the
// language of the source code - as determined by
// |clang_frontend_input_file.getKind().getLanguage()|).
class SomeClass;
struct MyStruct {
// No rewrite expected, because the path of this source file contains
// "third_party" substring.
SomeClass* ptr_field;
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment