Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: tools/gn/import_manager.cc

Issue 1886453002: GN: Use std::unique_ptr for owned pointers in ImportManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/import_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/gn/import_manager.h" 5 #include "tools/gn/import_manager.h"
6 6
7 #include <memory>
8
9 #include "base/stl_util.h"
10 #include "tools/gn/parse_tree.h" 7 #include "tools/gn/parse_tree.h"
11 #include "tools/gn/scheduler.h" 8 #include "tools/gn/scheduler.h"
12 #include "tools/gn/scope_per_file_provider.h" 9 #include "tools/gn/scope_per_file_provider.h"
13 10
14 namespace { 11 namespace {
15 12
16 // Returns a newly-allocated scope on success, null on failure. 13 // Returns a newly-allocated scope on success, null on failure.
17 Scope* UncachedImport(const Settings* settings, 14 std::unique_ptr<Scope> UncachedImport(const Settings* settings,
18 const SourceFile& file, 15 const SourceFile& file,
19 const ParseNode* node_for_err, 16 const ParseNode* node_for_err,
20 Err* err) { 17 Err* err) {
21 const ParseNode* node = g_scheduler->input_file_manager()->SyncLoadFile( 18 const ParseNode* node = g_scheduler->input_file_manager()->SyncLoadFile(
22 node_for_err->GetRange(), settings->build_settings(), file, err); 19 node_for_err->GetRange(), settings->build_settings(), file, err);
23 if (!node) 20 if (!node)
24 return nullptr; 21 return nullptr;
25 22
26 std::unique_ptr<Scope> scope(new Scope(settings->base_config())); 23 std::unique_ptr<Scope> scope(new Scope(settings->base_config()));
27 scope->set_source_dir(file.GetDir()); 24 scope->set_source_dir(file.GetDir());
28 25
29 // Don't allow ScopePerFileProvider to provide target-related variables. 26 // Don't allow ScopePerFileProvider to provide target-related variables.
30 // These will be relative to the imported file, which is probably not what 27 // These will be relative to the imported file, which is probably not what
31 // people mean when they use these. 28 // people mean when they use these.
32 ScopePerFileProvider per_file_provider(scope.get(), false); 29 ScopePerFileProvider per_file_provider(scope.get(), false);
33 30
34 scope->SetProcessingImport(); 31 scope->SetProcessingImport();
35 node->Execute(scope.get(), err); 32 node->Execute(scope.get(), err);
36 if (err->has_error()) 33 if (err->has_error())
37 return nullptr; 34 return nullptr;
38 scope->ClearProcessingImport(); 35 scope->ClearProcessingImport();
39 36
40 return scope.release(); 37 return scope;
41 } 38 }
42 39
43 } // namesapce 40 } // namesapce
44 41
45 ImportManager::ImportManager() { 42 ImportManager::ImportManager() {
46 } 43 }
47 44
48 ImportManager::~ImportManager() { 45 ImportManager::~ImportManager() {
49 STLDeleteContainerPairSecondPointers(imports_.begin(), imports_.end());
50 } 46 }
51 47
52 bool ImportManager::DoImport(const SourceFile& file, 48 bool ImportManager::DoImport(const SourceFile& file,
53 const ParseNode* node_for_err, 49 const ParseNode* node_for_err,
54 Scope* scope, 50 Scope* scope,
55 Err* err) { 51 Err* err) {
56 // See if we have a cached import, but be careful to actually do the scope 52 // See if we have a cached import, but be careful to actually do the scope
57 // copying outside of the lock. 53 // copying outside of the lock.
58 const Scope* imported_scope = nullptr; 54 const Scope* imported_scope = nullptr;
59 { 55 {
60 base::AutoLock lock(lock_); 56 base::AutoLock lock(lock_);
61 ImportMap::const_iterator found = imports_.find(file); 57 ImportMap::const_iterator found = imports_.find(file);
62 if (found != imports_.end()) 58 if (found != imports_.end())
63 imported_scope = found->second; 59 imported_scope = found->second.get();
64 } 60 }
65 61
66 if (!imported_scope) { 62 if (!imported_scope) {
67 // Do a new import of the file. 63 // Do a new import of the file.
68 imported_scope = UncachedImport(scope->settings(), file, 64 std::unique_ptr<Scope> new_imported_scope =
69 node_for_err, err); 65 UncachedImport(scope->settings(), file, node_for_err, err);
70 if (!imported_scope) 66 if (!new_imported_scope)
71 return false; 67 return false;
72 68
73 // We loaded the file outside the lock. This means that there could be a 69 // We loaded the file outside the lock. This means that there could be a
74 // race and the file was already loaded on a background thread. Recover 70 // race and the file was already loaded on a background thread. Recover
75 // from this and use the existing one if that happens. 71 // from this and use the existing one if that happens.
76 { 72 {
77 base::AutoLock lock(lock_); 73 base::AutoLock lock(lock_);
78 ImportMap::const_iterator found = imports_.find(file); 74 ImportMap::const_iterator found = imports_.find(file);
79 if (found != imports_.end()) { 75 if (found != imports_.end()) {
80 delete imported_scope; 76 imported_scope = found->second.get();
81 imported_scope = found->second;
82 } else { 77 } else {
83 imports_[file] = imported_scope; 78 imported_scope = new_imported_scope.get();
79 imports_[file] = std::move(new_imported_scope);
84 } 80 }
85 } 81 }
86 } 82 }
87 83
88 Scope::MergeOptions options; 84 Scope::MergeOptions options;
89 options.skip_private_vars = true; 85 options.skip_private_vars = true;
90 options.mark_dest_used = true; // Don't require all imported values be used. 86 options.mark_dest_used = true; // Don't require all imported values be used.
91 return imported_scope->NonRecursiveMergeTo(scope, options, node_for_err, 87 return imported_scope->NonRecursiveMergeTo(scope, options, node_for_err,
92 "import", err); 88 "import", err);
93 } 89 }
OLDNEW
« no previous file with comments | « tools/gn/import_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698